Skip to content

Commit a4bfaa3

Browse files
authored
feat: update solutions to lc problems: No.1209,1217,1218 (doocs#3501)
1 parent 919c842 commit a4bfaa3

File tree

6 files changed

+89
-91
lines changed

6 files changed

+89
-91
lines changed

solution/1200-1299/1209.Remove All Adjacent Duplicates in String II/README.md

+10-44
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ tags:
3939

4040
<pre><strong>输入:</strong>s = &quot;deeedbbcccbdaa&quot;, k = 3
4141
<strong>输出:</strong>&quot;aa&quot;
42-
<strong>解释:
42+
<strong>解释:
4343
</strong>先删除 &quot;eee&quot;&quot;ccc&quot;,得到 &quot;ddbbbdaa&quot;
4444
再删除 &quot;bbb&quot;,得到 &quot;dddaa&quot;
4545
最后删除 &quot;ddd&quot;,得到 &quot;aa&quot;</pre>
@@ -81,22 +81,15 @@ tags:
8181
```python
8282
class Solution:
8383
def removeDuplicates(self, s: str, k: int) -> str:
84-
t = []
85-
i, n = 0, len(s)
86-
while i < n:
87-
j = i
88-
while j < n and s[j] == s[i]:
89-
j += 1
90-
cnt = j - i
91-
cnt %= k
92-
if t and t[-1][0] == s[i]:
93-
t[-1][1] = (t[-1][1] + cnt) % k
94-
if t[-1][1] == 0:
95-
t.pop()
96-
elif cnt:
97-
t.append([s[i], cnt])
98-
i = j
99-
ans = [c * v for c, v in t]
84+
stk = []
85+
for c in s:
86+
if stk and stk[-1][0] == c:
87+
stk[-1][1] = (stk[-1][1] + 1) % k
88+
if stk[-1][1] == 0:
89+
stk.pop()
90+
else:
91+
stk.append([c, 1])
92+
ans = [c * v for c, v in stk]
10093
return "".join(ans)
10194
```
10295

@@ -190,31 +183,4 @@ type pair struct {
190183

191184
<!-- solution:end -->
192185

193-
<!-- solution:start -->
194-
195-
### 方法二
196-
197-
<!-- tabs:start -->
198-
199-
#### Python3
200-
201-
```python
202-
class Solution:
203-
def removeDuplicates(self, s: str, k: int) -> str:
204-
stk = []
205-
for c in s:
206-
if stk and stk[-1][0] == c:
207-
stk[-1][1] = (stk[-1][1] + 1) % k
208-
if stk[-1][1] == 0:
209-
stk.pop()
210-
else:
211-
stk.append([c, 1])
212-
ans = [c * v for c, v in stk]
213-
return "".join(ans)
214-
```
215-
216-
<!-- tabs:end -->
217-
218-
<!-- solution:end -->
219-
220186
<!-- problem:end -->

solution/1200-1299/1209.Remove All Adjacent Duplicates in String II/README_EN.md

+10-44
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ tags:
3838
<pre>
3939
<strong>Input:</strong> s = &quot;deeedbbcccbdaa&quot;, k = 3
4040
<strong>Output:</strong> &quot;aa&quot;
41-
<strong>Explanation:
41+
<strong>Explanation:
4242
</strong>First delete &quot;eee&quot; and &quot;ccc&quot;, get &quot;ddbbbdaa&quot;
4343
Then delete &quot;bbb&quot;, get &quot;dddaa&quot;
4444
Finally delete &quot;ddd&quot;, get &quot;aa&quot;</pre>
@@ -80,22 +80,15 @@ The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is
8080
```python
8181
class Solution:
8282
def removeDuplicates(self, s: str, k: int) -> str:
83-
t = []
84-
i, n = 0, len(s)
85-
while i < n:
86-
j = i
87-
while j < n and s[j] == s[i]:
88-
j += 1
89-
cnt = j - i
90-
cnt %= k
91-
if t and t[-1][0] == s[i]:
92-
t[-1][1] = (t[-1][1] + cnt) % k
93-
if t[-1][1] == 0:
94-
t.pop()
95-
elif cnt:
96-
t.append([s[i], cnt])
97-
i = j
98-
ans = [c * v for c, v in t]
83+
stk = []
84+
for c in s:
85+
if stk and stk[-1][0] == c:
86+
stk[-1][1] = (stk[-1][1] + 1) % k
87+
if stk[-1][1] == 0:
88+
stk.pop()
89+
else:
90+
stk.append([c, 1])
91+
ans = [c * v for c, v in stk]
9992
return "".join(ans)
10093
```
10194

@@ -189,31 +182,4 @@ type pair struct {
189182

190183
<!-- solution:end -->
191184

192-
<!-- solution:start -->
193-
194-
### Solution 2
195-
196-
<!-- tabs:start -->
197-
198-
#### Python3
199-
200-
```python
201-
class Solution:
202-
def removeDuplicates(self, s: str, k: int) -> str:
203-
stk = []
204-
for c in s:
205-
if stk and stk[-1][0] == c:
206-
stk[-1][1] = (stk[-1][1] + 1) % k
207-
if stk[-1][1] == 0:
208-
stk.pop()
209-
else:
210-
stk.append([c, 1])
211-
ans = [c * v for c, v in stk]
212-
return "".join(ans)
213-
```
214-
215-
<!-- tabs:end -->
216-
217-
<!-- solution:end -->
218-
219185
<!-- problem:end -->

solution/1200-1299/1217.Minimum Cost to Move Chips to The Same Position/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ tags:
8383

8484
将所有偶数下标的芯片移动到 0 号位置,所有奇数下标的芯片移动到 1 号位置,所有的代价为 0,接下来只需要在 0/1 号位置中选择其中一个较小数量的芯片,移动到另一个位置。所需的最小代价就是那个较小的数量。
8585

86-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为芯片的数量
86+
时间复杂度 $O(n)$,其中 $n$ 为芯片的数量。空间复杂度 $O(1)$。
8787

8888
<!-- tabs:start -->
8989

solution/1200-1299/1218.Longest Arithmetic Subsequence of Given Difference/README.md

+26-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,13 @@ tags:
6666

6767
### 方法一:动态规划
6868

69-
时间复杂度 $O(n)$。
69+
我们可以使用哈希表 $f$ 来存储以 $x$ 结尾的最长等差子序列的长度。
70+
71+
遍历数组 $\textit{arr}$,对于每个元素 $x$,我们更新 $f[x]$ 为 $f[x - \textit{difference}] + 1$。
72+
73+
遍历结束后,我们返回 $f$ 中的最大值作为答案返回即可。
74+
75+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{arr}$ 的长度。
7076

7177
<!-- tabs:start -->
7278

@@ -127,6 +133,25 @@ func longestSubsequence(arr []int, difference int) (ans int) {
127133
}
128134
```
129135

136+
#### Rust
137+
138+
```rust
139+
use std::collections::HashMap;
140+
141+
impl Solution {
142+
pub fn longest_subsequence(arr: Vec<i32>, difference: i32) -> i32 {
143+
let mut f = HashMap::new();
144+
let mut ans = 0;
145+
for &x in &arr {
146+
let count = f.get(&(x - difference)).unwrap_or(&0) + 1;
147+
f.insert(x, count);
148+
ans = ans.max(count);
149+
}
150+
ans
151+
}
152+
}
153+
```
154+
130155
#### TypeScript
131156

132157
```ts

solution/1200-1299/1218.Longest Arithmetic Subsequence of Given Difference/README_EN.md

+28-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,15 @@ tags:
6262

6363
<!-- solution:start -->
6464

65-
### Solution 1
65+
### Solution 1: Dynamic Programming
66+
67+
We can use a hash table $f$ to store the length of the longest arithmetic subsequence ending with $x$.
68+
69+
Traverse the array $\textit{arr}$, and for each element $x$, update $f[x]$ to be $f[x - \textit{difference}] + 1$.
70+
71+
After the traversal, return the maximum value in $f$ as the answer.
72+
73+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{arr}$.
6674

6775
<!-- tabs:start -->
6876

@@ -135,6 +143,25 @@ function longestSubsequence(arr: number[], difference: number): number {
135143
}
136144
```
137145

146+
#### Rust
147+
148+
```rust
149+
use std::collections::HashMap;
150+
151+
impl Solution {
152+
pub fn longest_subsequence(arr: Vec<i32>, difference: i32) -> i32 {
153+
let mut f = HashMap::new();
154+
let mut ans = 0;
155+
for &x in &arr {
156+
let count = f.get(&(x - difference)).unwrap_or(&0) + 1;
157+
f.insert(x, count);
158+
ans = ans.max(count);
159+
}
160+
ans
161+
}
162+
}
163+
```
164+
138165
#### JavaScript
139166

140167
```js
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn longest_subsequence(arr: Vec<i32>, difference: i32) -> i32 {
5+
let mut f = HashMap::new();
6+
let mut ans = 0;
7+
for &x in &arr {
8+
let count = f.get(&(x - difference)).unwrap_or(&0) + 1;
9+
f.insert(x, count);
10+
ans = ans.max(count);
11+
}
12+
ans
13+
}
14+
}

0 commit comments

Comments
 (0)