Skip to content

feat: update solutions to lc problems: No.1209,1217,1218 #3501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ tags:

<pre><strong>输入:</strong>s = &quot;deeedbbcccbdaa&quot;, k = 3
<strong>输出:</strong>&quot;aa&quot;
<strong>解释:
<strong>解释:
</strong>先删除 &quot;eee&quot; 和 &quot;ccc&quot;,得到 &quot;ddbbbdaa&quot;
再删除 &quot;bbb&quot;,得到 &quot;dddaa&quot;
最后删除 &quot;ddd&quot;,得到 &quot;aa&quot;</pre>
Expand Down Expand Up @@ -81,22 +81,15 @@ tags:
```python
class Solution:
def removeDuplicates(self, s: str, k: int) -> str:
t = []
i, n = 0, len(s)
while i < n:
j = i
while j < n and s[j] == s[i]:
j += 1
cnt = j - i
cnt %= k
if t and t[-1][0] == s[i]:
t[-1][1] = (t[-1][1] + cnt) % k
if t[-1][1] == 0:
t.pop()
elif cnt:
t.append([s[i], cnt])
i = j
ans = [c * v for c, v in t]
stk = []
for c in s:
if stk and stk[-1][0] == c:
stk[-1][1] = (stk[-1][1] + 1) % k
if stk[-1][1] == 0:
stk.pop()
else:
stk.append([c, 1])
ans = [c * v for c, v in stk]
return "".join(ans)
```

Expand Down Expand Up @@ -190,31 +183,4 @@ type pair struct {

<!-- solution:end -->

<!-- solution:start -->

### 方法二

<!-- tabs:start -->

#### Python3

```python
class Solution:
def removeDuplicates(self, s: str, k: int) -> str:
stk = []
for c in s:
if stk and stk[-1][0] == c:
stk[-1][1] = (stk[-1][1] + 1) % k
if stk[-1][1] == 0:
stk.pop()
else:
stk.append([c, 1])
ans = [c * v for c, v in stk]
return "".join(ans)
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ tags:
<pre>
<strong>Input:</strong> s = &quot;deeedbbcccbdaa&quot;, k = 3
<strong>Output:</strong> &quot;aa&quot;
<strong>Explanation:
<strong>Explanation:
</strong>First delete &quot;eee&quot; and &quot;ccc&quot;, get &quot;ddbbbdaa&quot;
Then delete &quot;bbb&quot;, get &quot;dddaa&quot;
Finally delete &quot;ddd&quot;, get &quot;aa&quot;</pre>
Expand Down Expand Up @@ -80,22 +80,15 @@ The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is
```python
class Solution:
def removeDuplicates(self, s: str, k: int) -> str:
t = []
i, n = 0, len(s)
while i < n:
j = i
while j < n and s[j] == s[i]:
j += 1
cnt = j - i
cnt %= k
if t and t[-1][0] == s[i]:
t[-1][1] = (t[-1][1] + cnt) % k
if t[-1][1] == 0:
t.pop()
elif cnt:
t.append([s[i], cnt])
i = j
ans = [c * v for c, v in t]
stk = []
for c in s:
if stk and stk[-1][0] == c:
stk[-1][1] = (stk[-1][1] + 1) % k
if stk[-1][1] == 0:
stk.pop()
else:
stk.append([c, 1])
ans = [c * v for c, v in stk]
return "".join(ans)
```

Expand Down Expand Up @@ -189,31 +182,4 @@ type pair struct {

<!-- solution:end -->

<!-- solution:start -->

### Solution 2

<!-- tabs:start -->

#### Python3

```python
class Solution:
def removeDuplicates(self, s: str, k: int) -> str:
stk = []
for c in s:
if stk and stk[-1][0] == c:
stk[-1][1] = (stk[-1][1] + 1) % k
if stk[-1][1] == 0:
stk.pop()
else:
stk.append([c, 1])
ans = [c * v for c, v in stk]
return "".join(ans)
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ tags:

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

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

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ tags:

### 方法一:动态规划

时间复杂度 $O(n)$。
我们可以使用哈希表 $f$ 来存储以 $x$ 结尾的最长等差子序列的长度。

遍历数组 $\textit{arr}$,对于每个元素 $x$,我们更新 $f[x]$ 为 $f[x - \textit{difference}] + 1$。

遍历结束后,我们返回 $f$ 中的最大值作为答案返回即可。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{arr}$ 的长度。

<!-- tabs:start -->

Expand Down Expand Up @@ -127,6 +133,25 @@ func longestSubsequence(arr []int, difference int) (ans int) {
}
```

#### Rust

```rust
use std::collections::HashMap;

impl Solution {
pub fn longest_subsequence(arr: Vec<i32>, difference: i32) -> i32 {
let mut f = HashMap::new();
let mut ans = 0;
for &x in &arr {
let count = f.get(&(x - difference)).unwrap_or(&0) + 1;
f.insert(x, count);
ans = ans.max(count);
}
ans
}
}
```

#### TypeScript

```ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,15 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Dynamic Programming

We can use a hash table $f$ to store the length of the longest arithmetic subsequence ending with $x$.

Traverse the array $\textit{arr}$, and for each element $x$, update $f[x]$ to be $f[x - \textit{difference}] + 1$.

After the traversal, return the maximum value in $f$ as the answer.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{arr}$.

<!-- tabs:start -->

Expand Down Expand Up @@ -135,6 +143,25 @@ function longestSubsequence(arr: number[], difference: number): number {
}
```

#### Rust

```rust
use std::collections::HashMap;

impl Solution {
pub fn longest_subsequence(arr: Vec<i32>, difference: i32) -> i32 {
let mut f = HashMap::new();
let mut ans = 0;
for &x in &arr {
let count = f.get(&(x - difference)).unwrap_or(&0) + 1;
f.insert(x, count);
ans = ans.max(count);
}
ans
}
}
```

#### JavaScript

```js
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use std::collections::HashMap;

impl Solution {
pub fn longest_subsequence(arr: Vec<i32>, difference: i32) -> i32 {
let mut f = HashMap::new();
let mut ans = 0;
for &x in &arr {
let count = f.get(&(x - difference)).unwrap_or(&0) + 1;
f.insert(x, count);
ans = ans.max(count);
}
ans
}
}
Loading