Skip to content

Commit f5fab82

Browse files
authored
feat: add solutions to lc problem: No.1974 (#4118)
No.1974.Minimum Time to Type Word Using Special Typewriter
1 parent 6b77fc8 commit f5fab82

File tree

8 files changed

+125
-107
lines changed

8 files changed

+125
-107
lines changed

solution/1900-1999/1974.Minimum Time to Type Word Using Special Typewriter/README.md

+41-34
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,13 @@ tags:
9393

9494
<!-- solution:start -->
9595

96-
### 方法一
96+
### 方法一:贪心
97+
98+
我们初始化答案变量 $\textit{ans}$ 为字符串的长度,表示我们至少需要 $\textit{ans}$ 秒来键入字符串。
99+
100+
接下来,我们遍历字符串,对于每个字符,我们计算当前字符和前一个字符之间的最小距离,将这个距离加到答案中。然后我们更新当前字符为前一个字符,继续遍历。
101+
102+
时间复杂度 $O(n)$,其中 $n$ 为字符串的长度。空间复杂度 $O(1)$。
97103

98104
<!-- tabs:start -->
99105

@@ -102,13 +108,11 @@ tags:
102108
```python
103109
class Solution:
104110
def minTimeToType(self, word: str) -> int:
105-
ans = prev = 0
106-
for c in word:
107-
curr = ord(c) - ord('a')
108-
t = abs(prev - curr)
109-
t = min(t, 26 - t)
110-
ans += t + 1
111-
prev = curr
111+
ans, a = len(word), ord("a")
112+
for c in map(ord, word):
113+
d = abs(c - a)
114+
ans += min(d, 26 - d)
115+
a = c
112116
return ans
113117
```
114118

@@ -117,14 +121,12 @@ class Solution:
117121
```java
118122
class Solution {
119123
public int minTimeToType(String word) {
120-
int ans = 0;
121-
int prev = 0;
124+
int ans = word.length();
125+
char a = 'a';
122126
for (char c : word.toCharArray()) {
123-
int curr = c - 'a';
124-
int t = Math.abs(prev - curr);
125-
t = Math.min(t, 26 - t);
126-
ans += t + 1;
127-
prev = curr;
127+
int d = Math.abs(a - c);
128+
ans += Math.min(d, 26 - d);
129+
a = c;
128130
}
129131
return ans;
130132
}
@@ -137,14 +139,12 @@ class Solution {
137139
class Solution {
138140
public:
139141
int minTimeToType(string word) {
140-
int ans = 0;
141-
int prev = 0;
142-
for (char& c : word) {
143-
int curr = c - 'a';
144-
int t = abs(prev - curr);
145-
t = min(t, 26 - t);
146-
ans += t + 1;
147-
prev = curr;
142+
int ans = word.length();
143+
char a = 'a';
144+
for (char c : word) {
145+
int d = abs(a - c);
146+
ans += min(d, 26 - d);
147+
a = c;
148148
}
149149
return ans;
150150
}
@@ -155,22 +155,29 @@ public:
155155
156156
```go
157157
func minTimeToType(word string) int {
158-
ans, prev := 0, 0
158+
ans := len(word)
159+
a := rune('a')
159160
for _, c := range word {
160-
curr := int(c - 'a')
161-
t := abs(prev - curr)
162-
t = min(t, 26-t)
163-
ans += t + 1
164-
prev = curr
161+
d := int(max(a-c, c-a))
162+
ans += min(d, 26-d)
163+
a = c
165164
}
166165
return ans
167166
}
167+
```
168168

169-
func abs(x int) int {
170-
if x < 0 {
171-
return -x
172-
}
173-
return x
169+
#### TypeScript
170+
171+
```ts
172+
function minTimeToType(word: string): number {
173+
let a = 'a'.charCodeAt(0);
174+
let ans = word.length;
175+
for (const c of word) {
176+
const d = Math.abs(c.charCodeAt(0) - a);
177+
ans += Math.min(d, 26 - d);
178+
a = c.charCodeAt(0);
179+
}
180+
return ans;
174181
}
175182
```
176183

solution/1900-1999/1974.Minimum Time to Type Word Using Special Typewriter/README_EN.md

+42-35
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ tags:
3636
<pre>
3737
<strong>Input:</strong> word = &quot;abc&quot;
3838
<strong>Output:</strong> 5
39-
<strong>Explanation:
39+
<strong>Explanation:
4040
</strong>The characters are printed as follows:
4141
- Type the character &#39;a&#39; in 1 second since the pointer is initially on &#39;a&#39;.
4242
- Move the pointer clockwise to &#39;b&#39; in 1 second.
@@ -91,7 +91,13 @@ The characters are printed as follows:
9191

9292
<!-- solution:start -->
9393

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)$.
95101

96102
<!-- tabs:start -->
97103

@@ -100,13 +106,11 @@ The characters are printed as follows:
100106
```python
101107
class Solution:
102108
def minTimeToType(self, word: str) -> int:
103-
ans = prev = 0
104-
for c in word:
105-
curr = ord(c) - ord('a')
106-
t = abs(prev - curr)
107-
t = min(t, 26 - t)
108-
ans += t + 1
109-
prev = curr
109+
ans, a = len(word), ord("a")
110+
for c in map(ord, word):
111+
d = abs(c - a)
112+
ans += min(d, 26 - d)
113+
a = c
110114
return ans
111115
```
112116

@@ -115,14 +119,12 @@ class Solution:
115119
```java
116120
class Solution {
117121
public int minTimeToType(String word) {
118-
int ans = 0;
119-
int prev = 0;
122+
int ans = word.length();
123+
char a = 'a';
120124
for (char c : word.toCharArray()) {
121-
int curr = c - 'a';
122-
int t = Math.abs(prev - curr);
123-
t = Math.min(t, 26 - t);
124-
ans += t + 1;
125-
prev = curr;
125+
int d = Math.abs(a - c);
126+
ans += Math.min(d, 26 - d);
127+
a = c;
126128
}
127129
return ans;
128130
}
@@ -135,14 +137,12 @@ class Solution {
135137
class Solution {
136138
public:
137139
int minTimeToType(string word) {
138-
int ans = 0;
139-
int prev = 0;
140-
for (char& c : word) {
141-
int curr = c - 'a';
142-
int t = abs(prev - curr);
143-
t = min(t, 26 - t);
144-
ans += t + 1;
145-
prev = curr;
140+
int ans = word.length();
141+
char a = 'a';
142+
for (char c : word) {
143+
int d = abs(a - c);
144+
ans += min(d, 26 - d);
145+
a = c;
146146
}
147147
return ans;
148148
}
@@ -153,22 +153,29 @@ public:
153153
154154
```go
155155
func minTimeToType(word string) int {
156-
ans, prev := 0, 0
156+
ans := len(word)
157+
a := rune('a')
157158
for _, c := range word {
158-
curr := int(c - 'a')
159-
t := abs(prev - curr)
160-
t = min(t, 26-t)
161-
ans += t + 1
162-
prev = curr
159+
d := int(max(a-c, c-a))
160+
ans += min(d, 26-d)
161+
a = c
163162
}
164163
return ans
165164
}
165+
```
166166

167-
func abs(x int) int {
168-
if x < 0 {
169-
return -x
170-
}
171-
return x
167+
#### TypeScript
168+
169+
```ts
170+
function minTimeToType(word: string): number {
171+
let a = 'a'.charCodeAt(0);
172+
let ans = word.length;
173+
for (const c of word) {
174+
const d = Math.abs(c.charCodeAt(0) - a);
175+
ans += Math.min(d, 26 - d);
176+
a = c.charCodeAt(0);
177+
}
178+
return ans;
172179
}
173180
```
174181

Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
class Solution {
22
public:
33
int minTimeToType(string word) {
4-
int ans = 0;
5-
int prev = 0;
6-
for (char& c : word) {
7-
int curr = c - 'a';
8-
int t = abs(prev - curr);
9-
t = min(t, 26 - t);
10-
ans += t + 1;
11-
prev = curr;
4+
int ans = word.length();
5+
char a = 'a';
6+
for (char c : word) {
7+
int d = abs(a - c);
8+
ans += min(d, 26 - d);
9+
a = c;
1210
}
1311
return ans;
1412
}
15-
};
13+
};
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
func minTimeToType(word string) int {
2-
ans, prev := 0, 0
2+
ans := len(word)
3+
a := rune('a')
34
for _, c := range word {
4-
curr := int(c - 'a')
5-
t := abs(prev - curr)
6-
t = min(t, 26-t)
7-
ans += t + 1
8-
prev = curr
5+
d := int(max(a-c, c-a))
6+
ans += min(d, 26-d)
7+
a = c
98
}
109
return ans
1110
}
12-
13-
func abs(x int) int {
14-
if x < 0 {
15-
return -x
16-
}
17-
return x
18-
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
class Solution {
22
public int minTimeToType(String word) {
3-
int ans = 0;
4-
int prev = 0;
3+
int ans = word.length();
4+
char a = 'a';
55
for (char c : word.toCharArray()) {
6-
int curr = c - 'a';
7-
int t = Math.abs(prev - curr);
8-
t = Math.min(t, 26 - t);
9-
ans += t + 1;
10-
prev = curr;
6+
int d = Math.abs(a - c);
7+
ans += Math.min(d, 26 - d);
8+
a = c;
119
}
1210
return ans;
1311
}
14-
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
class Solution:
22
def minTimeToType(self, word: str) -> int:
3-
ans = prev = 0
4-
for c in word:
5-
curr = ord(c) - ord('a')
6-
t = abs(prev - curr)
7-
t = min(t, 26 - t)
8-
ans += t + 1
9-
prev = curr
3+
ans, a = len(word), ord("a")
4+
for c in map(ord, word):
5+
d = abs(c - a)
6+
ans += min(d, 26 - d)
7+
a = c
108
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function minTimeToType(word: string): number {
2+
let a = 'a'.charCodeAt(0);
3+
let ans = word.length;
4+
for (const c of word) {
5+
const d = Math.abs(c.charCodeAt(0) - a);
6+
ans += Math.min(d, 26 - d);
7+
a = c.charCodeAt(0);
8+
}
9+
return ans;
10+
}

solution/1900-1999/1977.Number of Ways to Separate Numbers/README_EN.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,15 @@ tags:
6565

6666
<!-- solution:start -->
6767

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`.
6977

7078
<!-- tabs:start -->
7179

0 commit comments

Comments
 (0)