Skip to content

feat: update solutions to lc problems: No.0877,1859 #3256

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
Jul 11, 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
6 changes: 3 additions & 3 deletions solution/0800-0899/0877.Stone Game/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,16 @@ public:
int n = piles.size();
int f[n][n];
memset(f, 0, sizeof(f));
function<int(int, int)> dfs = [&](int i, int j) -> int {
auto dfs = [&](auto&& dfs, int i, int j) -> int {
if (i > j) {
return 0;
}
if (f[i][j]) {
return f[i][j];
}
return f[i][j] = max(piles[i] - dfs(i + 1, j), piles[j] - dfs(i, j - 1));
return f[i][j] = max(piles[i] - dfs(dfs, i + 1, j), piles[j] - dfs(dfs, i, j - 1));
};
return dfs(0, n - 1) > 0;
return dfs(dfs, 0, n - 1) > 0;
}
};
```
Expand Down
6 changes: 3 additions & 3 deletions solution/0800-0899/0877.Stone Game/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,16 @@ public:
int n = piles.size();
int f[n][n];
memset(f, 0, sizeof(f));
function<int(int, int)> dfs = [&](int i, int j) -> int {
auto dfs = [&](auto&& dfs, int i, int j) -> int {
if (i > j) {
return 0;
}
if (f[i][j]) {
return f[i][j];
}
return f[i][j] = max(piles[i] - dfs(i + 1, j), piles[j] - dfs(i, j - 1));
return f[i][j] = max(piles[i] - dfs(dfs, i + 1, j), piles[j] - dfs(dfs, i, j - 1));
};
return dfs(0, n - 1) > 0;
return dfs(dfs, 0, n - 1) > 0;
}
};
```
Expand Down
6 changes: 3 additions & 3 deletions solution/0800-0899/0877.Stone Game/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ class Solution {
int n = piles.size();
int f[n][n];
memset(f, 0, sizeof(f));
function<int(int, int)> dfs = [&](int i, int j) -> int {
auto dfs = [&](auto&& dfs, int i, int j) -> int {
if (i > j) {
return 0;
}
if (f[i][j]) {
return f[i][j];
}
return f[i][j] = max(piles[i] - dfs(i + 1, j), piles[j] - dfs(i, j - 1));
return f[i][j] = max(piles[i] - dfs(dfs, i + 1, j), piles[j] - dfs(dfs, i, j - 1));
};
return dfs(0, n - 1) > 0;
return dfs(dfs, 0, n - 1) > 0;
}
};
30 changes: 2 additions & 28 deletions solution/1800-1899/1859.Sorting the Sentence/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,9 @@ tags:

### 方法一:字符串分割

我们先将字符串 $s$ 按照空格分割,得到字符串数组 $words$。然后,我们创建一个长度为 $|words|$ 的字符串数组 $ans$,用于存放答案
我们先将字符串 $s$ 按照空格分割,得到字符串数组 $\textit{ws}$,然后遍历数组 $\textit{ws}$,将每个单词的最后一个字符减去字符 '1',得到的结果作为单词的索引,将单词的前缀作为单词的内容,最后将单词按照索引顺序拼接起来即可

接下来,遍历字符串数组 $words$ 中的每个字符串 $w$,找到 $w$ 的最后一个字符表示的位置 $i$,然后将 $w$ 的前 $|w|-1$ 个字符作为新的字符串 $w'$,将 $w'$ 放在数组 $ans$ 的第 $i$ 个位置。

最后,将数组 $ans$ 按照空格连接成字符串,即为答案。

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

<!-- tabs:start -->

Expand Down Expand Up @@ -176,26 +172,4 @@ var sortSentence = function (s) {

<!-- solution:end -->

<!-- solution:start -->

### 方法二

<!-- tabs:start -->

#### Python3

```python
class Solution:
def sortSentence(self, s: str) -> str:
ws = s.split()
ans = [None] * len(ws)
for w in ws:
ans[int(w[-1]) - 1] = w[:-1]
return ' '.join(ans)
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
38 changes: 7 additions & 31 deletions solution/1800-1899/1859.Sorting the Sentence/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,9 @@ tags:

### Solution 1: String Splitting

First, we split the string $s$ by spaces to get the string array $words$. Then, we create a string array $ans$ of length $|words|$ to store the answer.
First, we split the string $s$ by spaces to get the array of strings $\textit{ws}$. Then, we iterate through the array $\textit{ws}$, subtracting the character '1' from the last character of each word to get the result as the index of the word. We take the prefix of the word as the content of the word. Finally, we concatenate the words in index order.

Next, we iterate over each string $w$ in the string array $words$, find the position $i$ represented by the last character of $w$, then take the first $|w|-1$ characters of $w$ as the new string $w'$, and place $w'$ in the $i$th position of the array $ans$.

Finally, we join the array $ans$ into a string by spaces, which is the answer.

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

<!-- tabs:start -->

Expand All @@ -98,9 +94,11 @@ The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is
```python
class Solution:
def sortSentence(self, s: str) -> str:
ws = [(w[:-1], int(w[-1])) for w in s.split()]
ws.sort(key=lambda x: x[1])
return ' '.join(w for w, _ in ws)
ws = s.split()
ans = [None] * len(ws)
for w in ws:
ans[int(w[-1]) - 1] = w[:-1]
return " ".join(ans)
```

#### Java
Expand Down Expand Up @@ -193,26 +191,4 @@ var sortSentence = function (s) {

<!-- solution:end -->

<!-- solution:start -->

### Solution 2

<!-- tabs:start -->

#### Python3

```python
class Solution:
def sortSentence(self, s: str) -> str:
ws = s.split()
ans = [None] * len(ws)
for w in ws:
ans[int(w[-1]) - 1] = w[:-1]
return ' '.join(ans)
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
8 changes: 5 additions & 3 deletions solution/1800-1899/1859.Sorting the Sentence/Solution.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class Solution:
def sortSentence(self, s: str) -> str:
ws = [(w[:-1], int(w[-1])) for w in s.split()]
ws.sort(key=lambda x: x[1])
return ' '.join(w for w, _ in ws)
ws = s.split()
ans = [None] * len(ws)
for w in ws:
ans[int(w[-1]) - 1] = w[:-1]
return " ".join(ans)
7 changes: 0 additions & 7 deletions solution/1800-1899/1859.Sorting the Sentence/Solution2.py

This file was deleted.

Loading