Skip to content

[pull] main from doocs:main #1

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 2 commits into from
Dec 10, 2023
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 @@ -341,7 +341,7 @@ var longestValidParentheses = function (s) {
};
```

### Rust
### **Rust**

```rust
impl Solution {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ var longestValidParentheses = function (s) {
};
```

### Rust
### **Rust**

```rust
impl Solution {
Expand Down
40 changes: 40 additions & 0 deletions solution/0000-0099/0038.Count and Say/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ countAndSay(4) = 读 "21" = 一 个 2 + 一 个 1 = "12" + "11" = "1211"

## 解法

**方法一: 模拟**

题目要求输出第 $n$ 项的外观序列,而第 $n$ 项是序列中第 $n-1$ 项的描述。所以我们遍历 $n-1$ 次,每次迭代用快慢指针j和i,分别记录当前字符的位置以及下一个不等于当前字符的位置,更新上一项的序列为 $j-i$ 个当前字符。

时间复杂度:

1. 外部循环迭代 `for _ in range(n - 1)`,这会执行 `n-1` 次。
2. 在内部循环中,我们遍历了字符串`s`, 长度最大为上一项的长度。
3. 内部循环嵌套循环执行了一些基本操作,如比较和字符串拼接,这些基本操作的复杂度可以视为 $O(1)$ 。

综合考虑,整体时间复杂度为 $O(n \times m)$, 其中 n 是要生成的序列的项数, m 是前一项的最大长度。

空间复杂度: $O(m)$, 其中 m 是前一项的最大长度。

<!-- 这里可写通用的实现逻辑 -->

<!-- tabs:start -->
Expand Down Expand Up @@ -260,6 +274,32 @@ function countAndSay(n: number): string {
}
```

### **Rust**

```rust
use std::iter::once;

impl Solution {
pub fn count_and_say(n: i32) -> String {
(1..n)
.fold(vec![1], |curr, _| {
let mut next = vec![];
let mut slow = 0;
for fast in 0..=curr.len() {
if fast == curr.len() || curr[slow] != curr[fast] {
next.extend(once((fast - slow) as u8).chain(once(curr[slow])));
slow = fast;
}
}
next
})
.into_iter()
.map(|digit| (digit + b'0') as char)
.collect()
}
}
```

### **...**

```
Expand Down
40 changes: 40 additions & 0 deletions solution/0000-0099/0038.Count and Say/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ countAndSay(4) = say &quot;21&quot; = one 2 + one 1 = &quot;12&quot; + &quot;11&

## Solutions

**Solution 1: Simulation**

The task requires outputting the appearance sequence of the $n$-th item, where the $n$-th item is the description of the $n-1$-th item in the sequence. Therefore, we iterate $n-1$ times. In each iteration, we use fast and slow pointers, denoted as j and i respectively, to record the current character's position and the position of the next character that is not equal to the current character. We then update the sequence of the previous item to be $j-i$ occurrences of the current character.

Time Complexity:

1. The outer loop runs `n - 1` times, iterating to generate the "Count and Say" sequence up to the nth term.
2. The inner while loop iterates through each character in the current string s and counts the consecutive occurrences of the same character.
3. The inner while loop runs in $O(m)$ time, where m is the length of the current string s.

Overall, the time complexity is $O(n \times m)$, where n is the input parameter representing the term to generate, and m is the maximum length of the string in the sequence.

Space Complexity: $O(m)$.

<!-- tabs:start -->

### **Python3**
Expand Down Expand Up @@ -230,6 +244,32 @@ function countAndSay(n: number): string {
}
```

### **Rust**

```rust
use std::iter::once;

impl Solution {
pub fn count_and_say(n: i32) -> String {
(1..n)
.fold(vec![1], |curr, _| {
let mut next = vec![];
let mut slow = 0;
for fast in 0..=curr.len() {
if fast == curr.len() || curr[slow] != curr[fast] {
next.extend(once((fast - slow) as u8).chain(once(curr[slow])));
slow = fast;
}
}
next
})
.into_iter()
.map(|digit| (digit + b'0') as char)
.collect()
}
}
```

### **...**

```
Expand Down
22 changes: 22 additions & 0 deletions solution/0000-0099/0038.Count and Say/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use std::iter::once;

impl Solution {
pub fn count_and_say(n: i32) -> String {
(1..n)
.fold(vec![1], |curr, _| {
let mut next = vec![];
let mut slow = 0;
for fast in 0..=curr.len() {
if fast == curr.len() || curr[slow] != curr[fast] {
next.extend(once((fast - slow) as u8).chain(once(curr[slow])));
slow = fast;
}
}
next
})
.into_iter()
.map(|digit| (digit + b'0') as char)
.collect()
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -59,34 +59,106 @@

<!-- 这里可写通用的实现逻辑 -->

**方法一:双指针**

我们可以用两个指针 $j$ 和 $i$ 分别表示子数组的左右端点,初始时两个指针都指向数组的第一个元素。

接下来,我们遍历数组 $nums$ 中的每个元素 $x$,对于每个元素 $x$,我们将 $x$ 的出现次数加一,然后判断当前子数组是否满足要求。如果当前子数组不满足要求,我们就将指针 $j$ 右移一位,并将 $nums[j]$ 的出现次数减一,直到当前子数组满足要求为止。然后我们更新答案 $ans = \max(ans, i - j + 1)$。继续遍历,直到 $i$ 到达数组的末尾。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $nums$ 的长度。

<!-- tabs:start -->

### **Python3**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

class Solution:
def maxSubarrayLength(self, nums: List[int], k: int) -> int:
cnt = defaultdict(int)
ans = j = 0
for i, x in enumerate(nums):
cnt[x] += 1
while cnt[x] > k:
cnt[nums[j]] -= 1
j += 1
ans = max(ans, i - j + 1)
return ans
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```java

class Solution {
public int maxSubarrayLength(int[] nums, int k) {
Map<Integer, Integer> cnt = new HashMap<>();
int ans = 0;
for (int i = 0, j = 0; i < nums.length; ++i) {
cnt.merge(nums[i], 1, Integer::sum);
while (cnt.get(nums[i]) > k) {
cnt.merge(nums[j++], -1, Integer::sum);
}
ans = Math.max(ans, i - j + 1);
}
return ans;
}
}
```

### **C++**

```cpp

class Solution {
public:
int maxSubarrayLength(vector<int>& nums, int k) {
unordered_map<int, int> cnt;
int ans = 0;
for (int i = 0, j = 0; i < nums.size(); ++i) {
++cnt[nums[i]];
while (cnt[nums[i]] > k) {
--cnt[nums[j++]];
}
ans = max(ans, i - j + 1);
}
return ans;
}
};
```

### **Go**

```go
func maxSubarrayLength(nums []int, k int) (ans int) {
cnt := map[int]int{}
for i, j, n := 0, 0, len(nums); i < n; i++ {
cnt[nums[i]]++
for ; cnt[nums[i]] > k; j++ {
cnt[nums[j]]--
}
ans = max(ans, i-j+1)
}
return
}
```

### **TypeScript**

```ts
function maxSubarrayLength(nums: number[], k: number): number {
const cnt: Map<number, number> = new Map();
let ans = 0;
for (let i = 0, j = 0; i < nums.length; ++i) {
cnt.set(nums[i], (cnt.get(nums[i]) ?? 0) + 1);
for (; cnt.get(nums[i])! > k; ++j) {
cnt.set(nums[j], cnt.get(nums[j])! - 1);
}
ans = Math.max(ans, i - j + 1);
}
return ans;
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,30 +53,102 @@ It can be shown that there are no good subarrays with length more than 4.

## Solutions

**Solution 1: Two Pointers**

We can use two pointers $j$ and $i$ to represent the left and right endpoints of the subarray, initially both pointers point to the first element of the array.

Next, we iterate over each element $x$ in the array $nums$. For each element $x$, we increment the occurrence count of $x$, then check if the current subarray meets the requirements. If the current subarray does not meet the requirements, we move the pointer $j$ one step to the right, and decrement the occurrence count of $nums[j]$, until the current subarray meets the requirements. Then we update the answer $ans = \max(ans, i - j + 1)$. Continue the iteration until $i$ reaches the end of the array.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $nums$.

<!-- tabs:start -->

### **Python3**

```python

class Solution:
def maxSubarrayLength(self, nums: List[int], k: int) -> int:
cnt = defaultdict(int)
ans = j = 0
for i, x in enumerate(nums):
cnt[x] += 1
while cnt[x] > k:
cnt[nums[j]] -= 1
j += 1
ans = max(ans, i - j + 1)
return ans
```

### **Java**

```java

class Solution {
public int maxSubarrayLength(int[] nums, int k) {
Map<Integer, Integer> cnt = new HashMap<>();
int ans = 0;
for (int i = 0, j = 0; i < nums.length; ++i) {
cnt.merge(nums[i], 1, Integer::sum);
while (cnt.get(nums[i]) > k) {
cnt.merge(nums[j++], -1, Integer::sum);
}
ans = Math.max(ans, i - j + 1);
}
return ans;
}
}
```

### **C++**

```cpp

class Solution {
public:
int maxSubarrayLength(vector<int>& nums, int k) {
unordered_map<int, int> cnt;
int ans = 0;
for (int i = 0, j = 0; i < nums.size(); ++i) {
++cnt[nums[i]];
while (cnt[nums[i]] > k) {
--cnt[nums[j++]];
}
ans = max(ans, i - j + 1);
}
return ans;
}
};
```

### **Go**

```go
func maxSubarrayLength(nums []int, k int) (ans int) {
cnt := map[int]int{}
for i, j, n := 0, 0, len(nums); i < n; i++ {
cnt[nums[i]]++
for ; cnt[nums[i]] > k; j++ {
cnt[nums[j]]--
}
ans = max(ans, i-j+1)
}
return
}
```

### **TypeScript**

```ts
function maxSubarrayLength(nums: number[], k: number): number {
const cnt: Map<number, number> = new Map();
let ans = 0;
for (let i = 0, j = 0; i < nums.length; ++i) {
cnt.set(nums[i], (cnt.get(nums[i]) ?? 0) + 1);
for (; cnt.get(nums[i])! > k; ++j) {
cnt.set(nums[j], cnt.get(nums[j])! - 1);
}
ans = Math.max(ans, i - j + 1);
}
return ans;
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public:
int maxSubarrayLength(vector<int>& nums, int k) {
unordered_map<int, int> cnt;
int ans = 0;
for (int i = 0, j = 0; i < nums.size(); ++i) {
++cnt[nums[i]];
while (cnt[nums[i]] > k) {
--cnt[nums[j++]];
}
ans = max(ans, i - j + 1);
}
return ans;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
func maxSubarrayLength(nums []int, k int) (ans int) {
cnt := map[int]int{}
for i, j, n := 0, 0, len(nums); i < n; i++ {
cnt[nums[i]]++
for ; cnt[nums[i]] > k; j++ {
cnt[nums[j]]--
}
ans = max(ans, i-j+1)
}
return
}
Loading