Skip to content

[pull] main from doocs:main #374

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 3 commits into from
Feb 20, 2025
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
27 changes: 26 additions & 1 deletion solution/0700-0799/0772.Basic Calculator III/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,32 @@ tags:
#### Python3

```python

class Solution:
def calculate(self, s: str) -> int:
def dfs(q):
num, sign, stk = 0, "+", []
while q:
c = q.popleft()
if c.isdigit():
num = num * 10 + int(c)
if c == "(":
num = dfs(q)
if c in "+-*/)" or not q:
match sign:
case "+":
stk.append(num)
case "-":
stk.append(-num)
case "*":
stk.append(stk.pop() * num)
case "/":
stk.append(int(stk.pop() / num))
num, sign = 0, c
if c == ")":
break
return sum(stk)

return dfs(deque(s))
```

#### Java
Expand Down
27 changes: 26 additions & 1 deletion solution/0700-0799/0772.Basic Calculator III/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,32 @@ tags:
#### Python3

```python

class Solution:
def calculate(self, s: str) -> int:
def dfs(q):
num, sign, stk = 0, "+", []
while q:
c = q.popleft()
if c.isdigit():
num = num * 10 + int(c)
if c == "(":
num = dfs(q)
if c in "+-*/)" or not q:
match sign:
case "+":
stk.append(num)
case "-":
stk.append(-num)
case "*":
stk.append(stk.pop() * num)
case "/":
stk.append(int(stk.pop() / num))
num, sign = 0, c
if c == ")":
break
return sum(stk)

return dfs(deque(s))
```

#### Java
Expand Down
26 changes: 26 additions & 0 deletions solution/0700-0799/0772.Basic Calculator III/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution:
def calculate(self, s: str) -> int:
def dfs(q):
num, sign, stk = 0, "+", []
while q:
c = q.popleft()
if c.isdigit():
num = num * 10 + int(c)
if c == "(":
num = dfs(q)
if c in "+-*/)" or not q:
match sign:
case "+":
stk.append(num)
case "-":
stk.append(-num)
case "*":
stk.append(stk.pop() * num)
case "/":
stk.append(int(stk.pop() / num))
num, sign = 0, c
if c == ")":
break
return sum(stk)

return dfs(deque(s))
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,54 @@ public:
};
```

#### TypeScript

```ts
function getHappyString(n: number, k: number): string {
const ans: string[] = [];

const dfs = (s = '') => {
if (s.length === n) {
ans.push(s);
return;
}

for (const ch of 'abc') {
if (s.at(-1) === ch) continue;
dfs(s + ch);
}
};

dfs();

return ans[k - 1] ?? '';
}
```

#### JavaScript

```js
function getHappyString(n, k) {
const ans = [];

const dfs = (s = '') => {
if (s.length === n) {
ans.push(s);
return;
}

for (const ch of 'abc') {
if (s.at(-1) === ch) continue;
dfs(s + ch);
}
};

dfs();

return ans[k - 1] ?? '';
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,54 @@ public:
};
```

#### TypeScript

```ts
function getHappyString(n: number, k: number): string {
const ans: string[] = [];

const dfs = (s = '') => {
if (s.length === n) {
ans.push(s);
return;
}

for (const ch of 'abc') {
if (s.at(-1) === ch) continue;
dfs(s + ch);
}
};

dfs();

return ans[k - 1] ?? '';
}
```

#### JavaScript

```js
function getHappyString(n, k) {
const ans = [];

const dfs = (s = '') => {
if (s.length === n) {
ans.push(s);
return;
}

for (const ch of 'abc') {
if (s.at(-1) === ch) continue;
dfs(s + ch);
}
};

dfs();

return ans[k - 1] ?? '';
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function getHappyString(n, k) {
const ans = [];

const dfs = (s = '') => {
if (s.length === n) {
ans.push(s);
return;
}

for (const ch of 'abc') {
if (s.at(-1) === ch) continue;
dfs(s + ch);
}
};

dfs();

return ans[k - 1] ?? '';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function getHappyString(n: number, k: number): string {
const ans: string[] = [];

const dfs = (s = '') => {
if (s.length === n) {
ans.push(s);
return;
}

for (const ch of 'abc') {
if (s.at(-1) === ch) continue;
dfs(s + ch);
}
};

dfs();

return ans[k - 1] ?? '';
}
4 changes: 2 additions & 2 deletions solution/2300-2399/2330.Valid Palindrome IV/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ tags:
<strong>解释:</strong> 能让 s 变成回文,且只用了两步操作的方案如下:
- 将 s[0] 变成 'b' ,得到 s = "ba" 。
- 将 s[1] 变成 'b' ,得到s = "bb" 。
执行两步操作让 s 变成一个回文,所以返回 true 。
执行两步操作让 s 变成一个回文,所以返回 true 。
</pre>

<p><strong>示例 3:</strong></p>
Expand Down Expand Up @@ -69,7 +69,7 @@ tags:

### 方法一:双指针

我们可以使用双指针 $i$ 和 $j$,分别指向字符串的头尾,然后向中间移动,统计不同字符的个数,如果不同字符的个数大于 $2$,则返回 `false`,否则返回 `true`
我们可以使用双指针 $i$ 和 $j$,分别指向字符串的头尾,然后向中间移动,统计不同字符的个数,如果不同字符的个数大于 $2$,则返回 $\textit{false}$,否则返回 $\textit{true}$

时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $s$ 的长度。

Expand Down
6 changes: 5 additions & 1 deletion solution/2300-2399/2330.Valid Palindrome IV/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ Two operations could be performed to make s a palindrome so return true.

<!-- solution:start -->

### Solution 1
### Solution 1: Two Pointers

We can use two pointers $i$ and $j$, pointing to the beginning and end of the string, respectively, and then move towards the center, counting the number of different characters. If the number of different characters is greater than $2$, return $\textit{false}$; otherwise, return $\textit{true}$.

The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is the length of the string $s$.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,18 @@ Note that we cannot choose more than one edge because all edges are adjacent to

<!-- solution:start -->

### Solution 1
### Solution 1: Tree DP

We design a function $dfs(i)$, which represents the maximum sum of the weights of selected edges in the subtree rooted at node $i$, such that no two selected edges are adjacent. This function returns two values $(a, b)$. The first value $a$ represents the sum of the weights of selected edges when the edge between the current node $i$ and its parent node is selected. The second value $b$ represents the sum of the weights of selected edges when the edge between the current node $i$ and its parent node is not selected.

We can observe the following for the current node $i$:

- If the edge between $i$ and its parent node is selected, then none of the edges between $i$ and its child nodes can be selected. In this case, the value of $a$ for the current node is the sum of the $b$ values of all its child nodes.
- If the edge between $i$ and its parent node is not selected, then we can select at most one edge between $i$ and its child nodes. In this case, the value of $b$ for the current node is the sum of the $a$ values of the selected child nodes and the $b$ values of the unselected child nodes, plus the weight of the edge between $i$ and the selected child node.

We call the function $dfs(0)$, and the second value returned is the answer, which is the sum of the weights of selected edges when the edge between the root node and its parent node is not selected.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of nodes.

<!-- tabs:start -->

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

<!-- solution:start -->

### Solution 1
### Solution 1: Memoization Search

According to the problem description, we know that the number of 0s and 1s on the path from the top-left corner to the bottom-right corner is equal, and the total number is $m + n - 1$, which means the number of 0s and 1s are both $(m + n - 1) / 2$.

Therefore, we can use memoization search, starting from the top-left corner and moving right or down until reaching the bottom-right corner, to check if the number of 0s and 1s on the path is equal.

The time complexity is $O(m \times n \times (m + n))$. Here, $m$ and $n$ are the number of rows and columns of the matrix, respectively.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ tags:

### 方法一:线性筛

对于给定的范围 $[left, right]$,我们可以使用线性筛求出所有质数,然后从小到大遍历质数,找到相邻的两个质数,其差值最小的质数对即为答案。
对于给定的范围 $[\textit{left}, \textit{right}]$,我们可以使用线性筛求出所有质数,然后从小到大遍历质数,找到相邻的两个质数,其差值最小的质数对即为答案。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n = right$。
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n = \textit{right}$。

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ Since 11 is smaller than 17, we return the first pair.

<!-- solution:start -->

### Solution 1
### Solution 1: Linear Sieve

For the given range $[\textit{left}, \textit{right}]$, we can use the linear sieve method to find all prime numbers. Then, we traverse the prime numbers in ascending order to find the pair of adjacent prime numbers with the smallest difference, which will be the answer.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n = \textit{right}$.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ tags:

### 方法一:哈希表 + 滑动窗口 + 快速幂

我们用哈希表 `cnt` 维护窗口大小为 $k$ 的元素及其出现的次数。
我们用哈希表 $\textit{cnt}$ 维护窗口大小为 $k$ 的元素及其出现的次数。

先算出初始窗口为 $k$ 的所有元素的分数。然后利用滑动窗口,每次加入一个元素,并移除最左边的元素,同时利用快速幂更新分数。

时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 `nums` 的长度。
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $\textit{nums}$ 的长度。

<!-- tabs:start -->

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

<!-- solution:start -->

### Solution 1
### Solution 1: Hash Table + Sliding Window + Fast Power

We use a hash table $\textit{cnt}$ to maintain the elements of the window of size $k$ and their frequencies.

First, calculate the score of all elements in the initial window of size $k$. Then, use a sliding window to add one element at a time and remove the leftmost element, while updating the score using fast power.

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

<!-- tabs:start -->

Expand Down
Loading