diff --git a/solution/1900-1999/1974.Minimum Time to Type Word Using Special Typewriter/README.md b/solution/1900-1999/1974.Minimum Time to Type Word Using Special Typewriter/README.md index a2e36ebb0a709..2c6ea46b9bd3c 100644 --- a/solution/1900-1999/1974.Minimum Time to Type Word Using Special Typewriter/README.md +++ b/solution/1900-1999/1974.Minimum Time to Type Word Using Special Typewriter/README.md @@ -93,7 +93,13 @@ tags: -### 方法一 +### 方法一:贪心 + +我们初始化答案变量 $\textit{ans}$ 为字符串的长度,表示我们至少需要 $\textit{ans}$ 秒来键入字符串。 + +接下来,我们遍历字符串,对于每个字符,我们计算当前字符和前一个字符之间的最小距离,将这个距离加到答案中。然后我们更新当前字符为前一个字符,继续遍历。 + +时间复杂度 $O(n)$,其中 $n$ 为字符串的长度。空间复杂度 $O(1)$。 @@ -102,13 +108,11 @@ tags: ```python class Solution: def minTimeToType(self, word: str) -> int: - ans = prev = 0 - for c in word: - curr = ord(c) - ord('a') - t = abs(prev - curr) - t = min(t, 26 - t) - ans += t + 1 - prev = curr + ans, a = len(word), ord("a") + for c in map(ord, word): + d = abs(c - a) + ans += min(d, 26 - d) + a = c return ans ``` @@ -117,14 +121,12 @@ class Solution: ```java class Solution { public int minTimeToType(String word) { - int ans = 0; - int prev = 0; + int ans = word.length(); + char a = 'a'; for (char c : word.toCharArray()) { - int curr = c - 'a'; - int t = Math.abs(prev - curr); - t = Math.min(t, 26 - t); - ans += t + 1; - prev = curr; + int d = Math.abs(a - c); + ans += Math.min(d, 26 - d); + a = c; } return ans; } @@ -137,14 +139,12 @@ class Solution { class Solution { public: int minTimeToType(string word) { - int ans = 0; - int prev = 0; - for (char& c : word) { - int curr = c - 'a'; - int t = abs(prev - curr); - t = min(t, 26 - t); - ans += t + 1; - prev = curr; + int ans = word.length(); + char a = 'a'; + for (char c : word) { + int d = abs(a - c); + ans += min(d, 26 - d); + a = c; } return ans; } @@ -155,22 +155,29 @@ public: ```go func minTimeToType(word string) int { - ans, prev := 0, 0 + ans := len(word) + a := rune('a') for _, c := range word { - curr := int(c - 'a') - t := abs(prev - curr) - t = min(t, 26-t) - ans += t + 1 - prev = curr + d := int(max(a-c, c-a)) + ans += min(d, 26-d) + a = c } return ans } +``` -func abs(x int) int { - if x < 0 { - return -x - } - return x +#### TypeScript + +```ts +function minTimeToType(word: string): number { + let a = 'a'.charCodeAt(0); + let ans = word.length; + for (const c of word) { + const d = Math.abs(c.charCodeAt(0) - a); + ans += Math.min(d, 26 - d); + a = c.charCodeAt(0); + } + return ans; } ``` diff --git a/solution/1900-1999/1974.Minimum Time to Type Word Using Special Typewriter/README_EN.md b/solution/1900-1999/1974.Minimum Time to Type Word Using Special Typewriter/README_EN.md index 42963092b4003..8d19a81de9c63 100644 --- a/solution/1900-1999/1974.Minimum Time to Type Word Using Special Typewriter/README_EN.md +++ b/solution/1900-1999/1974.Minimum Time to Type Word Using Special Typewriter/README_EN.md @@ -36,7 +36,7 @@ tags:
 Input: word = "abc"
 Output: 5
-Explanation: 
+Explanation:
 The characters are printed as follows:
 - Type the character 'a' in 1 second since the pointer is initially on 'a'.
 - Move the pointer clockwise to 'b' in 1 second.
@@ -91,7 +91,13 @@ The characters are printed as follows:
 
 
 
-### Solution 1
+### Solution 1: Greedy
+
+We initialize the answer variable $\textit{ans}$ to the length of the string, indicating that we need at least $\textit{ans}$ seconds to type the string.
+
+Next, we traverse the string. For each character, we calculate the minimum distance between the current character and the previous character, and add this distance to the answer. Then we update the current character to the previous character and continue traversing.
+
+The time complexity is $O(n)$, where $n$ is the length of the string. The space complexity is $O(1)$.
 
 
 
@@ -100,13 +106,11 @@ The characters are printed as follows:
 ```python
 class Solution:
     def minTimeToType(self, word: str) -> int:
-        ans = prev = 0
-        for c in word:
-            curr = ord(c) - ord('a')
-            t = abs(prev - curr)
-            t = min(t, 26 - t)
-            ans += t + 1
-            prev = curr
+        ans, a = len(word), ord("a")
+        for c in map(ord, word):
+            d = abs(c - a)
+            ans += min(d, 26 - d)
+            a = c
         return ans
 ```
 
@@ -115,14 +119,12 @@ class Solution:
 ```java
 class Solution {
     public int minTimeToType(String word) {
-        int ans = 0;
-        int prev = 0;
+        int ans = word.length();
+        char a = 'a';
         for (char c : word.toCharArray()) {
-            int curr = c - 'a';
-            int t = Math.abs(prev - curr);
-            t = Math.min(t, 26 - t);
-            ans += t + 1;
-            prev = curr;
+            int d = Math.abs(a - c);
+            ans += Math.min(d, 26 - d);
+            a = c;
         }
         return ans;
     }
@@ -135,14 +137,12 @@ class Solution {
 class Solution {
 public:
     int minTimeToType(string word) {
-        int ans = 0;
-        int prev = 0;
-        for (char& c : word) {
-            int curr = c - 'a';
-            int t = abs(prev - curr);
-            t = min(t, 26 - t);
-            ans += t + 1;
-            prev = curr;
+        int ans = word.length();
+        char a = 'a';
+        for (char c : word) {
+            int d = abs(a - c);
+            ans += min(d, 26 - d);
+            a = c;
         }
         return ans;
     }
@@ -153,22 +153,29 @@ public:
 
 ```go
 func minTimeToType(word string) int {
-	ans, prev := 0, 0
+	ans := len(word)
+	a := rune('a')
 	for _, c := range word {
-		curr := int(c - 'a')
-		t := abs(prev - curr)
-		t = min(t, 26-t)
-		ans += t + 1
-		prev = curr
+		d := int(max(a-c, c-a))
+		ans += min(d, 26-d)
+		a = c
 	}
 	return ans
 }
+```
 
-func abs(x int) int {
-	if x < 0 {
-		return -x
-	}
-	return x
+#### TypeScript
+
+```ts
+function minTimeToType(word: string): number {
+    let a = 'a'.charCodeAt(0);
+    let ans = word.length;
+    for (const c of word) {
+        const d = Math.abs(c.charCodeAt(0) - a);
+        ans += Math.min(d, 26 - d);
+        a = c.charCodeAt(0);
+    }
+    return ans;
 }
 ```
 
diff --git a/solution/1900-1999/1974.Minimum Time to Type Word Using Special Typewriter/Solution.cpp b/solution/1900-1999/1974.Minimum Time to Type Word Using Special Typewriter/Solution.cpp
index 3965a90f69e05..617329aff3f46 100644
--- a/solution/1900-1999/1974.Minimum Time to Type Word Using Special Typewriter/Solution.cpp	
+++ b/solution/1900-1999/1974.Minimum Time to Type Word Using Special Typewriter/Solution.cpp	
@@ -1,15 +1,13 @@
 class Solution {
 public:
     int minTimeToType(string word) {
-        int ans = 0;
-        int prev = 0;
-        for (char& c : word) {
-            int curr = c - 'a';
-            int t = abs(prev - curr);
-            t = min(t, 26 - t);
-            ans += t + 1;
-            prev = curr;
+        int ans = word.length();
+        char a = 'a';
+        for (char c : word) {
+            int d = abs(a - c);
+            ans += min(d, 26 - d);
+            a = c;
         }
         return ans;
     }
-};
\ No newline at end of file
+};
diff --git a/solution/1900-1999/1974.Minimum Time to Type Word Using Special Typewriter/Solution.go b/solution/1900-1999/1974.Minimum Time to Type Word Using Special Typewriter/Solution.go
index d20aa96c8d2e0..a5ece08c75e60 100644
--- a/solution/1900-1999/1974.Minimum Time to Type Word Using Special Typewriter/Solution.go	
+++ b/solution/1900-1999/1974.Minimum Time to Type Word Using Special Typewriter/Solution.go	
@@ -1,18 +1,10 @@
 func minTimeToType(word string) int {
-	ans, prev := 0, 0
+	ans := len(word)
+	a := rune('a')
 	for _, c := range word {
-		curr := int(c - 'a')
-		t := abs(prev - curr)
-		t = min(t, 26-t)
-		ans += t + 1
-		prev = curr
+		d := int(max(a-c, c-a))
+		ans += min(d, 26-d)
+		a = c
 	}
 	return ans
 }
-
-func abs(x int) int {
-	if x < 0 {
-		return -x
-	}
-	return x
-}
\ No newline at end of file
diff --git a/solution/1900-1999/1974.Minimum Time to Type Word Using Special Typewriter/Solution.java b/solution/1900-1999/1974.Minimum Time to Type Word Using Special Typewriter/Solution.java
index 03409c4fc1f22..b9d8c249f4491 100644
--- a/solution/1900-1999/1974.Minimum Time to Type Word Using Special Typewriter/Solution.java	
+++ b/solution/1900-1999/1974.Minimum Time to Type Word Using Special Typewriter/Solution.java	
@@ -1,14 +1,12 @@
 class Solution {
     public int minTimeToType(String word) {
-        int ans = 0;
-        int prev = 0;
+        int ans = word.length();
+        char a = 'a';
         for (char c : word.toCharArray()) {
-            int curr = c - 'a';
-            int t = Math.abs(prev - curr);
-            t = Math.min(t, 26 - t);
-            ans += t + 1;
-            prev = curr;
+            int d = Math.abs(a - c);
+            ans += Math.min(d, 26 - d);
+            a = c;
         }
         return ans;
     }
-}
\ No newline at end of file
+}
diff --git a/solution/1900-1999/1974.Minimum Time to Type Word Using Special Typewriter/Solution.py b/solution/1900-1999/1974.Minimum Time to Type Word Using Special Typewriter/Solution.py
index e1557fcb6fbb8..8f4a6e8d7f36e 100644
--- a/solution/1900-1999/1974.Minimum Time to Type Word Using Special Typewriter/Solution.py	
+++ b/solution/1900-1999/1974.Minimum Time to Type Word Using Special Typewriter/Solution.py	
@@ -1,10 +1,8 @@
 class Solution:
     def minTimeToType(self, word: str) -> int:
-        ans = prev = 0
-        for c in word:
-            curr = ord(c) - ord('a')
-            t = abs(prev - curr)
-            t = min(t, 26 - t)
-            ans += t + 1
-            prev = curr
+        ans, a = len(word), ord("a")
+        for c in map(ord, word):
+            d = abs(c - a)
+            ans += min(d, 26 - d)
+            a = c
         return ans
diff --git a/solution/1900-1999/1974.Minimum Time to Type Word Using Special Typewriter/Solution.ts b/solution/1900-1999/1974.Minimum Time to Type Word Using Special Typewriter/Solution.ts
new file mode 100644
index 0000000000000..af408702ebd15
--- /dev/null
+++ b/solution/1900-1999/1974.Minimum Time to Type Word Using Special Typewriter/Solution.ts	
@@ -0,0 +1,10 @@
+function minTimeToType(word: string): number {
+    let a = 'a'.charCodeAt(0);
+    let ans = word.length;
+    for (const c of word) {
+        const d = Math.abs(c.charCodeAt(0) - a);
+        ans += Math.min(d, 26 - d);
+        a = c.charCodeAt(0);
+    }
+    return ans;
+}
diff --git a/solution/1900-1999/1977.Number of Ways to Separate Numbers/README_EN.md b/solution/1900-1999/1977.Number of Ways to Separate Numbers/README_EN.md
index dc2c5cfb1c97c..433ec8908c494 100644
--- a/solution/1900-1999/1977.Number of Ways to Separate Numbers/README_EN.md	
+++ b/solution/1900-1999/1977.Number of Ways to Separate Numbers/README_EN.md	
@@ -65,7 +65,15 @@ tags:
 
 
 
-### Solution 1
+### Solution 1: Dynamic Programming + Prefix Sum
+
+Define $dp[i][j]$ to represent the number of ways to partition the first $i$ characters of the string `num` such that the length of the last number is $j$. Clearly, the answer is $\sum_{j=0}^{n} dp[n][j]$. The initial value is $dp[0][0] = 1$.
+
+For $dp[i][j]$, the end of the previous number should be $i-j$. We can enumerate $dp[i-j][k]$, where $k \le j$. For the part where $k < j$, i.e., the number of ways with a length less than $j$ can be directly added to $dp[i][j]$, i.e., $dp[i][j] = \sum_{k=0}^{j-1} dp[i-j][k]$. Because the previous number is shorter, it means it is smaller than the current number. Here, prefix sum can be used for optimization.
+
+However, when $k = j$, we need to compare the sizes of the two numbers of the same length. If the previous number is larger than the current number, this situation is invalid, and we should not add it to $dp[i][j]$. Otherwise, we can add it to $dp[i][j]$. Here, we can preprocess the "longest common prefix" in $O(n^2)$ time, and then compare the sizes of two numbers of the same length in $O(1)$ time.
+
+The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$. Where $n$ is the length of the string `num`.