Skip to content

feat: add weekly contest 422 #3706

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
Nov 3, 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 @@ -22,7 +22,7 @@ tags:

<ol>
<li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>&quot; &quot;</code>).</li>
<li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity is neither present.</li>
<li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity if neither present.</li>
<li><strong>Conversion</strong>: Read the integer by skipping leading zeros&nbsp;until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.</li>
<li><strong>Rounding</strong>: If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then round the integer to remain in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be rounded to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be rounded to <code>2<sup>31</sup> - 1</code>.</li>
</ol>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ tags:
<ul>
<li><code>1 &lt;= target &lt;= 10<sup>9</sup></code></li>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
</ul>

<p>&nbsp;</p>
Expand Down
4 changes: 2 additions & 2 deletions solution/0700-0799/0729.My Calendar I/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ tags:

<p>当两个日程安排有一些时间上的交叉时(例如两个日程安排都在同一时间内),就会产生 <strong>重复预订</strong> 。</p>

<p>日程可以用一对整数 <code>start</code> 和 <code>end</code> 表示,这里的时间是半开区间,即 <code>[start, end)</code>, 实数&nbsp;<code>x</code> 的范围为, &nbsp;<code>start &lt;= x &lt; end</code> 。</p>
<p>日程可以用一对整数 <code>startTime</code> 和 <code>endTime</code> 表示,这里的时间是半开区间,即 <code>[startTime, endTime)</code>, 实数&nbsp;<code>x</code> 的范围为, &nbsp;<code>startTime &lt;= x &lt; endTime</code> 。</p>

<p>实现 <code>MyCalendar</code> 类:</p>

<ul>
<li><code>MyCalendar()</code> 初始化日历对象。</li>
<li><code>boolean book(int start, int end)</code> 如果可以将日程安排成功添加到日历中而不会导致重复预订,返回 <code>true</code> 。否则,返回 <code>false</code>&nbsp;并且不要将该日程安排添加到日历中。</li>
<li><code>boolean book(int startTime, int endTime)</code> 如果可以将日程安排成功添加到日历中而不会导致重复预订,返回 <code>true</code> 。否则,返回 <code>false</code>&nbsp;并且不要将该日程安排添加到日历中。</li>
</ul>

<p>&nbsp;</p>
Expand Down
46 changes: 25 additions & 21 deletions solution/0700-0799/0731.My Calendar II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,43 +21,47 @@ tags:

<!-- description:start -->

<p>实现一个 <code>MyCalendar</code> 类来存放你的日程安排。如果要添加的时间内不会导致三重预订时,则可以存储这个新的日程安排。</p>
<p>实现一个程序来存放你的日程安排。如果要添加的时间内不会导致三重预订时,则可以存储这个新的日程安排。</p>

<p><code>MyCalendar</code> 有一个 <code>book(int start, int end)</code>方法。它意味着在 <code>start</code> 到 <code>end</code> 时间内增加一个日程安排,注意,这里的时间是半开区间,即 <code>[start, end)</code>, 实数&nbsp;<code>x</code> 的范围为, &nbsp;<code>start &lt;= x &lt; end</code>。</p>
<p>当三个日程安排有一些时间上的交叉时(例如三个日程安排都在同一时间内),就会产生 <strong>三重预订</strong>。</p>

<p>当三个日程安排有一些时间上的交叉时(例如三个日程安排都在同一时间内),就会产生三重预订。</p>
<p>事件能够用一对整数&nbsp;<code>startTime</code>&nbsp;和&nbsp;<code>endTime</code>&nbsp;表示,在一个半开区间的时间&nbsp;<code>[startTime, endTime)</code>&nbsp;上预定。实数&nbsp;<code>x</code> 的范围为&nbsp;&nbsp;<code>startTime &lt;= x &lt; endTime</code>。</p>

<p>每次调用 <code>MyCalendar.book</code>方法时,如果可以将日程安排成功添加到日历中而不会导致三重预订,返回 <code>true</code>。否则,返回 <code>false</code> 并且不要将该日程安排添加到日历中。</p>
<p>实现&nbsp;<code>MyCalendarTwo</code> 类:</p>

<p>请按照以下步骤调用<code>MyCalendar</code> 类: <code>MyCalendar cal = new MyCalendar();</code> <code>MyCalendar.book(start, end)</code></p>
<ul>
<li><code>MyCalendarTwo()</code>&nbsp;初始化日历对象。</li>
<li><code>boolean book(int startTime, int endTime)</code>&nbsp;如果可以将日程安排成功添加到日历中而不会导致三重预订,返回 <code>true</code>。否则,返回 <code>false</code> 并且不要将该日程安排添加到日历中。</li>
</ul>

<p>&nbsp;</p>

<p><strong class="example">示例:</strong></p>
<p><strong class="example">示例 1:</strong></p>

<pre>
MyCalendar();
MyCalendar.book(10, 20); // returns true
MyCalendar.book(50, 60); // returns true
MyCalendar.book(10, 40); // returns true
MyCalendar.book(5, 15); // returns false
MyCalendar.book(5, 10); // returns true
MyCalendar.book(25, 55); // returns true
<strong>解释:</strong>
前两个日程安排可以添加至日历中。 第三个日程安排会导致双重预订,但可以添加至日历中。
第四个日程安排活动(5,15)不能添加至日历中,因为它会导致三重预订。
第五个日程安排(5,10)可以添加至日历中,因为它未使用已经双重预订的时间10。
第六个日程安排(25,55)可以添加至日历中,因为时间 [25,40] 将和第三个日程安排双重预订;
时间 [40,50] 将单独预订,时间 [50,55)将和第二个日程安排双重预订。
<strong>输入:</strong>
["MyCalendarTwo", "book", "book", "book", "book", "book", "book"]
[[], [10, 20], [50, 60], [10, 40], [5, 15], [5, 10], [25, 55]]
<strong>输出:</strong>
[null, true, true, true, false, true, true]

<strong>解释:</strong>
MyCalendarTwo myCalendarTwo = new MyCalendarTwo();
myCalendarTwo.book(10, 20); // 返回 True,能够预定该日程。
myCalendarTwo.book(50, 60); // 返回 True,能够预定该日程。
myCalendarTwo.book(10, 40); // 返回 True,该日程能够被重复预定。
myCalendarTwo.book(5, 15); // 返回 False,该日程导致了三重预定,所以不能预定。
myCalendarTwo.book(5, 10); // 返回 True,能够预定该日程,因为它不使用已经双重预订的时间 10。
myCalendarTwo.book(25, 55); // 返回 True,能够预定该日程,因为时间段 [25, 40) 将被第三个日程重复预定,时间段 [40, 50) 将被单独预定,而时间段 [50, 55) 将被第二个日程重复预定。
</pre>

<p>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
<li>每个测试用例,调用&nbsp;<code>MyCalendar.book</code>&nbsp;函数最多不超过&nbsp;<code>1000</code>次。</li>
<li>调用函数&nbsp;<code>MyCalendar.book(start, end)</code>时,&nbsp;<code>start</code> 和&nbsp;<code>end</code> 的取值范围为&nbsp;<code>[0, 10^9]</code>。</li>
<li><code>0 &lt;= start &lt; end &lt;= 10<sup>9</sup></code></li>
<li>最多调用&nbsp;<code>book</code>&nbsp;1000 次。</li>
</ul>

<!-- description:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ tags:
<pre>
<strong>Input:</strong> s = &quot;001101&quot;
<strong>Output:</strong> 6
<strong>Explanation:</strong>
<strong>Explanation:</strong>
The following sets of indices selected are valid:
- [0,2,4] from &quot;<u><strong>0</strong></u>0<strong><u>1</u></strong>1<strong><u>0</u></strong>1&quot; forms &quot;010&quot;
- [0,3,4] from &quot;<u><strong>0</strong></u>01<u><strong>10</strong></u>1&quot; forms &quot;010&quot;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ tags:
<strong>输入:</strong>s = "100011001", k = 3
<strong>输出:</strong>"11001"
<strong>解释:</strong>示例中共有 7 个美丽子字符串:
1. 子字符串 "<em><strong>100011</strong></em>001" 。
2. 子字符串 "<strong><em>1000110</em></strong>01" 。
3. 子字符串 "<strong><em>100011001</em></strong>" 。
4. 子字符串 "1<strong><em>00011001</em></strong>" 。
5. 子字符串 "10<strong><em>0011001</em></strong>" 。
6. 子字符串 "100<em><strong>011001</strong></em>" 。
7. 子字符串 "1000<strong><em>11001</em></strong>" 。
1. 子字符串 "<u>100011</u>001" 。
2. 子字符串 "<u>1000110</u>01" 。
3. 子字符串 "<u>10001100</u>1" 。
4. 子字符串 "1<u>00011001</u>" 。
5. 子字符串 "10<u>0011001</u>" 。
6. 子字符串 "100<u>011001</u>" 。
7. 子字符串 "1000<u>11001</u>" 。
最短美丽子字符串的长度是 5 。
长度为 5 且字典序最小的美丽子字符串是子字符串 "11001" 。
</pre>
Expand All @@ -58,9 +58,9 @@ tags:
<strong>输入:</strong>s = "1011", k = 2
<strong>输出:</strong>"11"
<strong>解释:</strong>示例中共有 3 个美丽子字符串:
1. 子字符串 "<em><strong>101</strong></em>1" 。
2. 子字符串 "1<em><strong>011</strong></em>" 。
3. 子字符串 "10<em><strong>11</strong></em>" 。
1. 子字符串 "<u>101</u>1" 。
2. 子字符串 "1<u>011</u>" 。
3. 子字符串 "10<u>11</u>" 。
最短美丽子字符串的长度是 2 。
长度为 2 且字典序最小的美丽子字符串是子字符串 "11" 。
</pre>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ rating: 1352
source: 第 405 场周赛 Q2
tags:
- 位运算
- 递归
- 字符串
- 回溯
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ rating: 1352
source: Weekly Contest 405 Q2
tags:
- Bit Manipulation
- Recursion
- String
- Backtracking
---

<!-- problem:start -->
Expand Down
28 changes: 14 additions & 14 deletions solution/3300-3399/3322.Premier League Table Ranking III/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,20 @@ tags:
<p><strong>输出:</strong></p>

<pre>
+------------+---------+-------------------+--------+-----------------+------+
| season_id | team_id | team_name | points | goal_difference | rank |
+------------+---------+-------------------+--------+-----------------+------+
| 2021 | 1 | Manchester City | 93 | 73 | 1 |
| 2021 | 2 | Liverpool | 92 | 68 | 2 |
| 2021 | 3 | Chelsea | 74 | 43 | 3 |
| 2021 | 4 | Tottenham | 71 | 29 | 4 |
| 2021 | 5 | Arsenal | 69 | 13 | 5 |
| 2022 | 1 | Manchester City | 89 | 61 | 1 |
| 2022 | 2 | Arsenal | 84 | 45 | 2 |
| 2022 | 3 | Manchester United | 75 | 15 | 3 |
| 2022 | 4 | Newcastle | 71 | 35 | 4 |
| 2022 | 5 | Liverpool | 67 | 28 | 5 |
+------------+---------+-------------------+--------+-----------------+------+
+------------+---------+-------------------+--------+-----------------+----------+
| season_id | team_id | team_name | points | goal_difference | position |
+------------+---------+-------------------+--------+-----------------+----------+
| 2021 | 1 | Manchester City | 93 | 73 | 1 |
| 2021 | 2 | Liverpool | 92 | 68 | 2 |
| 2021 | 3 | Chelsea | 74 | 43 | 3 |
| 2021 | 4 | Tottenham | 71 | 29 | 4 |
| 2021 | 5 | Arsenal | 69 | 13 | 5 |
| 2022 | 1 | Manchester City | 89 | 61 | 1 |
| 2022 | 2 | Arsenal | 84 | 45 | 2 |
| 2022 | 3 | Manchester United | 75 | 15 | 3 |
| 2022 | 4 | Newcastle | 71 | 35 | 4 |
| 2022 | 5 | Liverpool | 67 | 28 | 5 |
+------------+---------+-------------------+--------+-----------------+----------+
</pre>

<p><strong>解释:</strong></p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ tags:
<li><code>(arr[i] * arr[i + 1]) - arr[i] - arr[i + 1]</code>&nbsp;是偶数。</li>
</ul>

<p>返回大小为 <code>n</code>&nbsp;的满足&nbsp;<strong>K 偶数</strong> 的数组的数量,其中所有元素的范围在&nbsp;<code>[1, m]</code>。</p>
<p>返回长度为 <code>n</code>&nbsp;的满足&nbsp;<strong>K 偶数</strong> 的数组的数量,其中所有元素的范围在&nbsp;<code>[1, m]</code>。</p>

<p>因为答案可能很大,返回答案对&nbsp;<code>10<sup>9</sup> + 7</code>&nbsp;取模。</p>

Expand Down
144 changes: 144 additions & 0 deletions solution/3300-3399/3340.Check Balanced String/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
---
comments: true
difficulty: 简单
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3340.Check%20Balanced%20String/README.md
---

<!-- problem:start -->

# [3340. 检查平衡字符串](https://leetcode.cn/problems/check-balanced-string)

[English Version](/solution/3300-3399/3340.Check%20Balanced%20String/README_EN.md)

## 题目描述

<!-- description:start -->

<p>给你一个仅由数字 0 - 9 组成的字符串 <code>num</code>。如果偶数下标处的数字之和等于奇数下标处的数字之和,则认为该数字字符串是一个 <b>平衡字符串</b>。</p>

<p>如果 <code>num</code> 是一个 <strong>平衡字符串</strong>,则返回 <code>true</code>;否则,返回 <code>false</code>。</p>

<p>&nbsp;</p>

<p><strong class="example">示例 1:</strong></p>

<div class="example-block">
<p><strong>输入:</strong>num<span class="example-io"> = "1234"</span></p>

<p><strong>输出:</strong><span class="example-io">false</span></p>

<p><strong>解释:</strong></p>

<ul>
<li>偶数下标处的数字之和为 <code>1 + 3 = 4</code>,奇数下标处的数字之和为 <code>2 + 4 = 6</code>。</li>
<li>由于 4 不等于 6,<code>num</code> 不是平衡字符串。</li>
</ul>
</div>

<p><strong class="example">示例 2:</strong></p>

<div class="example-block">
<p><strong>输入:</strong>num<span class="example-io"> = "24123"</span></p>

<p><strong>输出:</strong>true</p>

<p><strong>解释:</strong></p>

<ul>
<li>偶数下标处的数字之和为 <code>2 + 1 + 3 = 6</code>,奇数下标处的数字之和为 <code>4 + 2 = 6</code>。</li>
<li>由于两者相等,<code>num</code> 是平衡字符串。</li>
</ul>
</div>

<p>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
<li><code>2 &lt;= num.length &lt;= 100</code></li>
<li><code>num</code> 仅由数字 0 - 9 组成。</li>
</ul>

<!-- description:end -->

## 解法

<!-- solution:start -->

### 方法一:模拟

我们可以用一个长度为 $2$ 的数组 $f$ 来记录偶数下标和奇数下标的数字之和,然后遍历字符串 $\textit{nums}$,根据下标的奇偶性将数字加到对应的位置上,最后判断 $f[0]$ 是否等于 $f[1]$ 即可。

时间复杂度 $O(n)$,其中 $n$ 为字符串 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

#### Python3

```python
class Solution:
def isBalanced(self, num: str) -> bool:
f = [0, 0]
for i, x in enumerate(map(int, num)):
f[i & 1] += x
return f[0] == f[1]
```

#### Java

```java
class Solution {
public boolean isBalanced(String num) {
int[] f = new int[2];
for (int i = 0; i < num.length(); ++i) {
f[i & 1] += num.charAt(i) - '0';
}
return f[0] == f[1];
}
}
```

#### C++

```cpp
class Solution {
public:
bool isBalanced(string num) {
int f[2]{};
for (int i = 0; i < num.size(); ++i) {
f[i & 1] += num[i] - '0';
}
return f[0] == f[1];
}
};
```

#### Go

```go
func isBalanced(num string) bool {
f := [2]int{}
for i, c := range num {
f[i&1] += int(c - '0')
}
return f[0] == f[1]
}
```

#### TypeScript

```ts
function isBalanced(num: string): boolean {
const f = [0, 0];
for (let i = 0; i < num.length; ++i) {
f[i & 1] += +num[i];
}
return f[0] === f[1];
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Loading
Loading