You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/1900-1999/1974.Minimum Time to Type Word Using Special Typewriter/README_EN.md
+42-35
Original file line number
Diff line number
Diff line change
@@ -36,7 +36,7 @@ tags:
36
36
<pre>
37
37
<strong>Input:</strong> word = "abc"
38
38
<strong>Output:</strong> 5
39
-
<strong>Explanation:
39
+
<strong>Explanation:
40
40
</strong>The characters are printed as follows:
41
41
- Type the character 'a' in 1 second since the pointer is initially on 'a'.
42
42
- Move the pointer clockwise to 'b' in 1 second.
@@ -91,7 +91,13 @@ The characters are printed as follows:
91
91
92
92
<!-- solution:start -->
93
93
94
-
### Solution 1
94
+
### Solution 1: Greedy
95
+
96
+
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.
97
+
98
+
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.
99
+
100
+
The time complexity is $O(n)$, where $n$ is the length of the string. The space complexity is $O(1)$.
95
101
96
102
<!-- tabs:start -->
97
103
@@ -100,13 +106,11 @@ The characters are printed as follows:
Copy file name to clipboardexpand all lines: solution/1900-1999/1977.Number of Ways to Separate Numbers/README_EN.md
+9-1
Original file line number
Diff line number
Diff line change
@@ -65,7 +65,15 @@ tags:
65
65
66
66
<!-- solution:start -->
67
67
68
-
### Solution 1
68
+
### Solution 1: Dynamic Programming + Prefix Sum
69
+
70
+
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$.
71
+
72
+
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.
73
+
74
+
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.
75
+
76
+
The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$. Where $n$ is the length of the string `num`.
0 commit comments