Skip to content

[pull] main from doocs:main #183

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 5 commits into from
Sep 25, 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
54,696 changes: 27,401 additions & 27,295 deletions images/starcharts.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions solution/0000-0099/0063.Unique Paths II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ tags:

<!-- description:start -->

<p>一个机器人位于一个<meta charset="UTF-8" />&nbsp;<code>m x n</code>&nbsp;网格的左上角 (起始点在下图中标记为 “Start” )。</p>
<p>给定一个&nbsp;<code>m x n</code>&nbsp;的整数数组&nbsp;<code>grid</code>。一个机器人初始位于 <strong>左上角</strong>(即 <code>grid[0][0]</code>)。机器人尝试移动到 <strong>右下角</strong>(即 <code>grid[m - 1][n - 1]</code>)。机器人每次只能向下或者向右移动一步。</p>

<p>机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为 “Finish”)。</p>
<p>网格中的障碍物和空位置分别用 <code>1</code> 和 <code>0</code> 来表示。机器人的移动路径中不能包含 <strong>任何</strong>&nbsp;有障碍物的方格。</p>

<p>现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?</p>
<p>返回机器人能够到达右下角的不同路径数量。</p>

<p>网格中的障碍物和空位置分别用 <code>1</code> 和 <code>0</code> 来表示。</p>
<p>测试用例保证答案小于等于 <code>2 * 10<sup>9</sup></code>。</p>

<p>&nbsp;</p>

Expand Down
2 changes: 1 addition & 1 deletion solution/0100-0199/0191.Number of 1 Bits/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ tags:
<pre>
<strong>输入:</strong>n = 2147483645
<strong>输出:</strong>30
<strong>解释:</strong>输入的二进制串 <strong>11111111111111111111111111111101</strong> 中,共有 30 个设置位。</pre>
<strong>解释:</strong>输入的二进制串 <strong>1111111111111111111111111111101</strong> 中,共有 30 个设置位。</pre>

<p>&nbsp;</p>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,24 @@ tags:

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0300-0399/0314.Binary%20Tree%20Vertical%20Order%20Traversal/images/vtree1.jpg" style="width: 282px; height: 301px;" />
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0300-0399/0314.Binary%20Tree%20Vertical%20Order%20Traversal/images/image1.png" style="width: 400px; height: 273px;" />
<pre>
<strong>Input:</strong> root = [3,9,20,null,null,15,7]
<strong>Output:</strong> [[9],[3,15],[20],[7]]
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0300-0399/0314.Binary%20Tree%20Vertical%20Order%20Traversal/images/vtree2-1.jpg" style="width: 462px; height: 222px;" />
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0300-0399/0314.Binary%20Tree%20Vertical%20Order%20Traversal/images/image3.png" style="width: 450px; height: 285px;" />
<pre>
<strong>Input:</strong> root = [3,9,8,4,0,1,7]
<strong>Output:</strong> [[4],[9],[3,0,1],[8],[7]]
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0300-0399/0314.Binary%20Tree%20Vertical%20Order%20Traversal/images/vtree2.jpg" style="width: 462px; height: 302px;" />
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0300-0399/0314.Binary%20Tree%20Vertical%20Order%20Traversal/images/image2.png" style="width: 350px; height: 342px;" />
<pre>
<strong>Input:</strong> root = [3,9,8,4,0,1,7,null,null,null,2,5]
<strong>Output:</strong> [[4],[9,5],[3,0,1],[8,2],[7]]
<strong>Input:</strong> root = [1,2,3,4,10,9,11,null,5,null,null,null,null,null,null,null,6]
<strong>Output:</strong> [[4],[2,5],[1,10,9,6],[3],[11]]
</pre>

<p>&nbsp;</p>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ tags:

<p>&nbsp;</p>

<p><img src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0400-0499/0428.Serialize%20and%20Deserialize%20N-ary%20Tree/images/narytreeexample.png" style="height: 321px; width: 500px;" /></p>
<p><img src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0400-0499/0428.Serialize%20and%20Deserialize%20N-ary%20Tree/images/1727093143-BPVnoI-image.png" style="height: 321px; width: 500px;" /></p>

<p>&nbsp;</p>

<p>为&nbsp;<code>[1 [3[5 6] 2 4]]</code>。你不需要以这种形式完成,你可以自己创造和实现不同的方法。</p>

<p>或者,您可以遵循 LeetCode 的层序遍历序列化格式,其中每组孩子节点由空值分隔。</p>

<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0400-0499/0428.Serialize%20and%20Deserialize%20N-ary%20Tree/images/sample_4_964.png" style="height: 454px; width: 500px;" /></p>
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0400-0499/0428.Serialize%20and%20Deserialize%20N-ary%20Tree/images/1727093169-WGFOps-image.png" style="height: 454px; width: 500px;" /></p>

<p>例如,上面的树可以序列化为 <code>[1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]</code></p>

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions solution/0900-0999/0929.Unique Email Addresses/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ tags:
<li>每个 <code>emails[i]</code> 都包含有且仅有一个 <code>'@'</code> 字符</li>
<li>所有本地名和域名都不为空</li>
<li>本地名不会以 <code>'+'</code> 字符作为开头</li>
<li>域名以&nbsp;<code>".com"</code> 后缀结尾。</li>
<li>域名在&nbsp;<code>".com"</code> 后缀前至少包含一个字符</li>
</ul>

<!-- description:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ tags:
<li>All local and domain names are non-empty.</li>
<li>Local names do not start with a <code>&#39;+&#39;</code> character.</li>
<li>Domain names end with the <code>&quot;.com&quot;</code> suffix.</li>
<li>Domain names must contain at least one character before <code>&quot;.com&quot;</code> suffix.</li>
</ul>

<!-- description:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ tags:
<p>各查询结果的评分与其位置之间比率的平均值。</p>
</blockquote>

<p>将劣质查询百分比&nbsp;<code>poor_query_percentage</code>:</p>
<p>将劣质查询百分比&nbsp;<code>poor_query_percentage</code>&nbsp;定义为:</p>

<blockquote>
<p>评分小于 3 的查询结果占全部查询结果的百分比。</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ tags:

### 方法一:考虑开始、结束的位置关系

由于每个阶段的结束位置是下一个阶段的开始位置,并且每个阶段都是逆时针方向的,所以我们可以根据开始和结束的位置关系来确定每个扇区的经过次数。

如果 $\textit{rounds}[0] \leq \textit{rounds}[m]$,那么从 $\textit{rounds}[0]$ 开始,到 $\textit{rounds}[m]$ 结束的所有扇区经过的次数是最多的,我们可以直接返回这个区间内的所有扇区。

否则,从 $1$ 开始,到 $\textit{rounds}[m]$ 结束的所有扇区和从 $\textit{rounds}[0]$ 开始,到 $n$ 结束的所有扇区的并集是经过次数最多的,我们可以返回这两个区间的并集。

时间复杂度 $O(n)$,其中 $n$ 是扇区的个数。忽略答案数组的空间消耗,空间复杂度 $O(1)$。

<!-- tabs:start -->

#### Python3
Expand Down Expand Up @@ -114,10 +122,16 @@ public:
int m = rounds.size() - 1;
vector<int> ans;
if (rounds[0] <= rounds[m]) {
for (int i = rounds[0]; i <= rounds[m]; ++i) ans.push_back(i);
for (int i = rounds[0]; i <= rounds[m]; ++i) {
ans.push_back(i);
}
} else {
for (int i = 1; i <= rounds[m]; ++i) ans.push_back(i);
for (int i = rounds[0]; i <= n; ++i) ans.push_back(i);
for (int i = 1; i <= rounds[m]; ++i) {
ans.push_back(i);
}
for (int i = rounds[0]; i <= n; ++i) {
ans.push_back(i);
}
}
return ans;
}
Expand Down Expand Up @@ -146,6 +160,28 @@ func mostVisited(n int, rounds []int) []int {
}
```

#### TypeScript

```ts
function mostVisited(n: number, rounds: number[]): number[] {
const ans: number[] = [];
const m = rounds.length - 1;
if (rounds[0] <= rounds[m]) {
for (let i = rounds[0]; i <= rounds[m]; ++i) {
ans.push(i);
}
} else {
for (let i = 1; i <= rounds[m]; ++i) {
ans.push(i);
}
for (let i = rounds[0]; i <= n; ++i) {
ans.push(i);
}
}
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,15 @@ We can see that both sectors 1 and 2 are visited twice and they are the most vis

<!-- solution:start -->

### Solution 1
### Solution 1: Considering the Relationship Between Start and End Positions

Since the end position of each stage is the start position of the next stage, and each stage is in a counterclockwise direction, we can determine the number of times each sector is passed based on the relationship between the start and end positions.

If $\textit{rounds}[0] \leq \textit{rounds}[m]$, then the sectors from $\textit{rounds}[0]$ to $\textit{rounds}[m]$ are passed the most times, and we can directly return all sectors within this interval.

Otherwise, the sectors from $1$ to $\textit{rounds}[m]$ and the sectors from $\textit{rounds}[0]$ to $n$ form the union of the most passed sectors, and we can return the union of these two intervals.

The time complexity is $O(n)$, where $n$ is the number of sectors. Ignoring the space consumption of the answer array, the space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -113,10 +121,16 @@ public:
int m = rounds.size() - 1;
vector<int> ans;
if (rounds[0] <= rounds[m]) {
for (int i = rounds[0]; i <= rounds[m]; ++i) ans.push_back(i);
for (int i = rounds[0]; i <= rounds[m]; ++i) {
ans.push_back(i);
}
} else {
for (int i = 1; i <= rounds[m]; ++i) ans.push_back(i);
for (int i = rounds[0]; i <= n; ++i) ans.push_back(i);
for (int i = 1; i <= rounds[m]; ++i) {
ans.push_back(i);
}
for (int i = rounds[0]; i <= n; ++i) {
ans.push_back(i);
}
}
return ans;
}
Expand Down Expand Up @@ -145,6 +159,28 @@ func mostVisited(n int, rounds []int) []int {
}
```

#### TypeScript

```ts
function mostVisited(n: number, rounds: number[]): number[] {
const ans: number[] = [];
const m = rounds.length - 1;
if (rounds[0] <= rounds[m]) {
for (let i = rounds[0]; i <= rounds[m]; ++i) {
ans.push(i);
}
} else {
for (let i = 1; i <= rounds[m]; ++i) {
ans.push(i);
}
for (let i = rounds[0]; i <= n; ++i) {
ans.push(i);
}
}
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ class Solution {
int m = rounds.size() - 1;
vector<int> ans;
if (rounds[0] <= rounds[m]) {
for (int i = rounds[0]; i <= rounds[m]; ++i) ans.push_back(i);
for (int i = rounds[0]; i <= rounds[m]; ++i) {
ans.push_back(i);
}
} else {
for (int i = 1; i <= rounds[m]; ++i) ans.push_back(i);
for (int i = rounds[0]; i <= n; ++i) ans.push_back(i);
for (int i = 1; i <= rounds[m]; ++i) {
ans.push_back(i);
}
for (int i = rounds[0]; i <= n; ++i) {
ans.push_back(i);
}
}
return ans;
}
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function mostVisited(n: number, rounds: number[]): number[] {
const ans: number[] = [];
const m = rounds.length - 1;
if (rounds[0] <= rounds[m]) {
for (let i = rounds[0]; i <= rounds[m]; ++i) {
ans.push(i);
}
} else {
for (let i = 1; i <= rounds[m]; ++i) {
ans.push(i);
}
for (let i = rounds[0]; i <= n; ++i) {
ans.push(i);
}
}
return ans;
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@ tags:

<!-- solution:start -->

### 方法一:贪心
### 方法一:贪心 + 排序

Bob 取走最小的 1/3,剩余的硬币堆由 Alice 和我按硬币数从高到低依次取走每一堆。
为了让我们获得的硬币数量最多,我们可以贪心地让 Bob 拿走最少的 $n$ 堆硬币。我们每次先让 Alice 拿走最多的一堆硬币,然后让我们拿走第二多的一堆硬币,依次循环,直到没有硬币可拿。

时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是硬币堆数。

<!-- tabs:start -->

Expand All @@ -88,18 +90,17 @@ Bob 取走最小的 1/3,剩余的硬币堆由 Alice 和我按硬币数从高
class Solution:
def maxCoins(self, piles: List[int]) -> int:
piles.sort()
return sum(piles[-2 : len(piles) // 3 - 1 : -2])
return sum(piles[len(piles) // 3 :][::2])
```

#### Java

```java
class Solution {

public int maxCoins(int[] piles) {
Arrays.sort(piles);
int ans = 0;
for (int i = piles.length - 2; i >= piles.length / 3; i -= 2) {
for (int i = piles.length / 3; i < piles.length; i += 2) {
ans += piles[i];
}
return ans;
Expand All @@ -113,9 +114,11 @@ class Solution {
class Solution {
public:
int maxCoins(vector<int>& piles) {
sort(piles.begin(), piles.end());
ranges::sort(piles);
int ans = 0;
for (int i = piles.size() - 2; i >= (int) piles.size() / 3; i -= 2) ans += piles[i];
for (int i = piles.size() / 3; i < piles.size(); i += 2) {
ans += piles[i];
}
return ans;
}
};
Expand All @@ -124,13 +127,12 @@ public:
#### Go

```go
func maxCoins(piles []int) int {
func maxCoins(piles []int) (ans int) {
sort.Ints(piles)
ans, n := 0, len(piles)
for i := n - 2; i >= n/3; i -= 2 {
for i := len(piles) / 3; i < len(piles); i += 2 {
ans += piles[i]
}
return ans
return
}
```

Expand All @@ -139,10 +141,9 @@ func maxCoins(piles []int) int {
```ts
function maxCoins(piles: number[]): number {
piles.sort((a, b) => a - b);
const n = piles.length;
let ans = 0;
for (let i = 1; i <= Math.floor(n / 3); i++) {
ans += piles[n - 2 * i];
for (let i = piles.length / 3; i < piles.length; i += 2) {
ans += piles[i];
}
return ans;
}
Expand All @@ -154,10 +155,9 @@ function maxCoins(piles: number[]): number {
impl Solution {
pub fn max_coins(mut piles: Vec<i32>) -> i32 {
piles.sort();
let n = piles.len();
let mut ans = 0;
for i in 1..=n / 3 {
ans += piles[n - 2 * i];
for i in (piles.len() / 3..piles.len()).step_by(2) {
ans += piles[i];
}
ans
}
Expand All @@ -167,16 +167,16 @@ impl Solution {
#### C

```c
int cmp(const void* a, const void* b) {
return *(int*) a - *(int*) b;
int compare(const void* a, const void* b) {
return (*(int*) a - *(int*) b);
}

int maxCoins(int* piles, int pilesSize) {
qsort(piles, pilesSize, sizeof(int), cmp);
qsort(piles, pilesSize, sizeof(int), compare);
int ans = 0;
for (int i = 1; i <= pilesSize / 3; i++) {
ans += piles[pilesSize - 2 * i];
};
for (int i = pilesSize / 3; i < pilesSize; i += 2) {
ans += piles[i];
}
return ans;
}
```
Expand Down
Loading