Skip to content

Commit

Permalink
feat: add weekly contest 427 and biweekly contest 145 (#3846)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme authored Dec 8, 2024
1 parent 8a2235a commit 2ee3b36
Show file tree
Hide file tree
Showing 53 changed files with 2,341 additions and 46 deletions.
1 change: 1 addition & 0 deletions solution/0300-0399/0355.Design Twitter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ twitter.getNewsFeed(1); // 用户 1 获取推文应当返回一个列表,其
<li><code>0 &lt;= tweetId &lt;= 10<sup>4</sup></code></li>
<li>所有推特的 ID 都互不相同</li>
<li><code>postTweet</code>、<code>getNewsFeed</code>、<code>follow</code> 和 <code>unfollow</code> 方法最多调用 <code>3 * 10<sup>4</sup></code> 次</li>
<li>用户不能关注自己</li>
</ul>

<!-- description:end -->
Expand Down
1 change: 1 addition & 0 deletions solution/0300-0399/0355.Design Twitter/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ twitter.getNewsFeed(1); // User 1&#39;s news feed should return a list with 1 t
<li><code>0 &lt;= tweetId &lt;= 10<sup>4</sup></code></li>
<li>All the tweets have <strong>unique</strong> IDs.</li>
<li>At most <code>3 * 10<sup>4</sup></code> calls will be made to <code>postTweet</code>, <code>getNewsFeed</code>, <code>follow</code>, and <code>unfollow</code>.</li>
<li>A user cannot follow himself.</li>
</ul>

<!-- description:end -->
Expand Down
2 changes: 1 addition & 1 deletion solution/0700-0799/0782.Transform to Chessboard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ tags:

若 $n$ 为奇数,那么最终的合法棋盘只有一种可能。如果第一行中 $0$ 的数目大于 $1$,那么最终一盘的第一行只能是“01010...”,否则就是“10101...”。同样算出次数作为答案。

时间复杂度 $O(n^2)$。
时间复杂度 $O(n^2)$,其中 $n$ 是棋盘的大小。空间复杂度 $O(1)$

<!-- tabs:start -->

Expand Down
19 changes: 18 additions & 1 deletion solution/0700-0799/0782.Transform to Chessboard/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,24 @@ The second move swaps the second and third row.

<!-- solution:start -->

### Solution 1
### Solution 1: Pattern Observation + State Compression

In a valid chessboard, there are exactly two types of "rows".

For example, if one row on the chessboard is "01010011", then any other row can only be "01010011" or "10101100". Columns also satisfy this property.

Additionally, each row and each column has half $0$s and half $1$s. Suppose the chessboard is $n \times n$:

- If $n = 2 \times k$, then each row and each column has $k$ $1$s and $k$ $0$s.
- If $n = 2 \times k + 1$, then each row has $k$ $1$s and $k + 1$ $0$s, or $k + 1$ $1$s and $k$ $0$s.

Based on the above conclusions, we can determine whether a chessboard is valid. If valid, we can calculate the minimum number of moves required.

If $n$ is even, there are two possible valid chessboards, where the first row is either "010101..." or "101010...". We calculate the minimum number of swaps required for these two possibilities and take the smaller value as the answer.

If $n$ is odd, there is only one possible valid chessboard. If the number of $0$s in the first row is greater than the number of $1$s, then the first row of the final chessboard must be "01010..."; otherwise, it must be "10101...". We calculate the number of swaps required and use it as the answer.

The time complexity is $O(n^2)$, where $n$ is the size of the chessboard. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down
2 changes: 1 addition & 1 deletion solution/0800-0899/0809.Expressive Words/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ tags:
<p><strong>示例:</strong></p>

<pre>
<strong>输入:</strong>
<strong>输入:</strong>
s = "heeellooo"
words = ["hello", "hi", "helo"]
<strong>输出:</strong>1
Expand Down
2 changes: 1 addition & 1 deletion solution/0800-0899/0809.Expressive Words/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ tags:
<pre>
<strong>Input:</strong> s = &quot;heeellooo&quot;, words = [&quot;hello&quot;, &quot;hi&quot;, &quot;helo&quot;]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
<strong>Explanation:</strong>
We can extend &quot;e&quot; and &quot;o&quot; in the word &quot;hello&quot; to get &quot;heeellooo&quot;.
We can&#39;t extend &quot;helo&quot; to get &quot;heeellooo&quot; because the group &quot;ll&quot; is not size 3 or more.
</pre>
Expand Down
2 changes: 1 addition & 1 deletion solution/0800-0899/0810.Chalkboard XOR Game/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ tags:
<pre>
<strong>输入:</strong> nums = [1,1,2]
<strong>输出:</strong> false
<strong>解释:</strong>
<strong>解释:</strong>
Alice 有两个选择: 擦掉数字 1 或 2。
如果擦掉 1, 数组变成 [1, 2]。剩余数字按位异或得到 1 XOR 2 = 3。那么 Bob 可以擦掉任意数字,因为 Alice 会成为擦掉最后一个数字的人,她总是会输。
如果 Alice 擦掉 2,那么数组变成[1, 1]。剩余数字按位异或得到 1 XOR 1 = 0。Alice 仍然会输掉游戏。
Expand Down
6 changes: 3 additions & 3 deletions solution/0800-0899/0810.Chalkboard XOR Game/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ tags:
<pre>
<strong>Input:</strong> nums = [1,1,2]
<strong>Output:</strong> false
<strong>Explanation:</strong>
Alice has two choices: erase 1 or erase 2.
If she erases 1, the nums array becomes [1, 2]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 2 = 3. Now Bob can remove any element he wants, because Alice will be the one to erase the last element and she will lose.
<strong>Explanation:</strong>
Alice has two choices: erase 1 or erase 2.
If she erases 1, the nums array becomes [1, 2]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 2 = 3. Now Bob can remove any element he wants, because Alice will be the one to erase the last element and she will lose.
If Alice erases 2 first, now nums become [1, 1]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 1 = 0. Alice will lose.
</pre>

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

<p>注意:车不能穿过其它棋子,比如象和卒。这意味着如果有其它棋子挡住了路径,车就不能够吃掉棋子。</p>

<p>返回白车将能 <strong>吃掉</strong><strong>卒的数量</strong>。</p>
<p>返回白车 <strong>攻击</strong>&nbsp;范围内 <strong>兵的数量</strong>。</p>

<p>&nbsp;</p>

Expand Down
2 changes: 1 addition & 1 deletion solution/1000-1099/1004.Max Consecutive Ones III/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ tags:

<!-- description:start -->

<p>给定一个二进制数组&nbsp;<code>nums</code>&nbsp;和一个整数 <code>k</code>,如果可以翻转最多 <code>k</code> 个 <code>0</code> ,则返回 <em>数组中连续 <code>1</code> 的最大个数</em> 。</p>
<p>给定一个二进制数组&nbsp;<code>nums</code>&nbsp;和一个整数 <code>k</code>,假设最多可以翻转 <code>k</code> 个 <code>0</code> ,则返回执行操作后 <em>数组中连续 <code>1</code> 的最大个数</em> 。</p>

<p>&nbsp;</p>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ tags:
<strong>输出:</strong>2
<strong>解释:</strong>
对于 arr1[0]=4 我们有:
|4-10|=6 &gt; d=2
|4-9|=5 &gt; d=2
|4-1|=3 &gt; d=2
|4-8|=4 &gt; d=2
|4-10|=6 &gt; d=2
|4-9|=5 &gt; d=2
|4-1|=3 &gt; d=2
|4-8|=4 &gt; d=2
所以 arr1[0]=4 符合距离要求

对于 arr1[1]=5 我们有:
|5-10|=5 &gt; d=2
|5-9|=4 &gt; d=2
|5-1|=4 &gt; d=2
|5-10|=5 &gt; d=2
|5-9|=4 &gt; d=2
|5-1|=4 &gt; d=2
|5-8|=3 &gt; d=2
所以 arr1[1]=5 也符合距离要求

Expand All @@ -51,7 +51,7 @@ tags:
<strong>|8-9|=1 &lt;= d=2</strong>
|8-1|=7 &gt; d=2
<strong>|8-8|=0 &lt;= d=2</strong>
存在距离小于等于 2 的情况,不符合距离要求
存在距离小于等于 2 的情况,不符合距离要求

故而只有 arr1[0]=4 和 arr1[1]=5 两个符合距离要求,距离值为 2</pre>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ tags:
<pre>
<strong>Input:</strong> arr1 = [4,5,8], arr2 = [10,9,1,8], d = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong>
For arr1[0]=4 we have:
|4-10|=6 &gt; d=2
|4-9|=5 &gt; d=2
|4-1|=3 &gt; d=2
|4-8|=4 &gt; d=2
For arr1[1]=5 we have:
|5-10|=5 &gt; d=2
|5-9|=4 &gt; d=2
|5-1|=4 &gt; d=2
<strong>Explanation:</strong>
For arr1[0]=4 we have:
|4-10|=6 &gt; d=2
|4-9|=5 &gt; d=2
|4-1|=3 &gt; d=2
|4-8|=4 &gt; d=2
For arr1[1]=5 we have:
|5-10|=5 &gt; d=2
|5-9|=4 &gt; d=2
|5-1|=4 &gt; d=2
|5-8|=3 &gt; d=2
For arr1[2]=8 we have:
<strong>|8-10|=2 &lt;= d=2</strong>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ tags:

<p>有一个&nbsp;<code>8 x 8</code>&nbsp;的棋盘,它包含&nbsp;<code>n</code>&nbsp;个棋子(棋子包括车,后和象三种)。给你一个长度为 <code>n</code>&nbsp;的字符串数组&nbsp;<code>pieces</code>&nbsp;,其中&nbsp;<code>pieces[i]</code>&nbsp;表示第 <code>i</code>&nbsp;个棋子的类型(车,后或象)。除此以外,还给你一个长度为 <code>n</code>&nbsp;的二维整数数组&nbsp;<code>positions</code>&nbsp;,其中 <code>positions[i] = [r<sub>i</sub>, c<sub>i</sub>]</code>&nbsp;表示第 <code>i</code>&nbsp;个棋子现在在棋盘上的位置为&nbsp;<code>(r<sub>i</sub>, c<sub>i</sub>)</code>&nbsp;,棋盘下标从 <strong>1</strong>&nbsp;开始。</p>

<p>棋盘上每个棋子都可以移动 <b>至多一次</b>&nbsp;每个棋子的移动中,首先选择移动的 <strong>方向</strong>&nbsp;,然后选择 <strong>移动的步数</strong>&nbsp;,同时你要确保移动过程中棋子不能移到棋盘以外的地方。棋子需按照以下规则移动:</p>
<p>每个棋子的移动中,首先选择移动的 <strong>方向</strong>&nbsp;,然后选择 <strong>移动的步数</strong>&nbsp;,同时你要确保移动过程中棋子不能移到棋盘以外的地方。棋子需按照以下规则移动:</p>

<ul>
<li>车可以 <strong>水平或者竖直</strong>&nbsp;从&nbsp;<code>(r, c)</code>&nbsp;沿着方向&nbsp;<code>(r+1, c)</code>,<code>(r-1, c)</code>,<code>(r, c+1)</code>&nbsp;或者&nbsp;<code>(r, c-1)</code>&nbsp;移动。</li>
<li>后可以 <strong>水平竖直或者斜对角</strong>&nbsp;从&nbsp;<code>(r, c)</code> 沿着方向&nbsp;<code>(r+1, c)</code>,<code>(r-1, c)</code>,<code>(r, c+1)</code>,<code>(r, c-1)</code>,<code>(r+1, c+1)</code>,<code>(r+1, c-1)</code>,<code>(r-1, c+1)</code>,<code>(r-1, c-1)</code>&nbsp;移动。</li>
<li>象可以 <strong>斜对角</strong>&nbsp;从&nbsp;<code>(r, c)</code>&nbsp;沿着方向&nbsp;<code>(r+1, c+1)</code>,<code>(r+1, c-1)</code>,<code>(r-1, c+1)</code>,<code>(r-1, c-1)</code>&nbsp;移动。</li>
</ul>

<p><strong>移动组合</strong>&nbsp;包含所有棋子的 <strong>移动</strong>&nbsp;。每一秒,每个棋子都沿着它们选择的方向往前移动 <strong>一步</strong>&nbsp;,直到它们到达目标位置。所有棋子从时刻 <code>0</code>&nbsp;开始移动。如果在某个时刻,两个或者更多棋子占据了同一个格子,那么这个移动组合 <strong>不有效</strong>&nbsp;。</p>
<p>你必须同时 <strong>移动</strong> 棋盘上的每一个棋子。<strong>移动组合</strong>&nbsp;包含所有棋子的 <strong>移动</strong>&nbsp;。每一秒,每个棋子都沿着它们选择的方向往前移动 <strong>一步</strong>&nbsp;,直到它们到达目标位置。所有棋子从时刻 <code>0</code>&nbsp;开始移动。如果在某个时刻,两个或者更多棋子占据了同一个格子,那么这个移动组合 <strong>不有效</strong>&nbsp;。</p>

<p>请你返回 <strong>有效</strong>&nbsp;移动组合的数目。</p>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ tags:
<pre>
<strong>Input:</strong> nums = [0,1,2]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
<strong>Explanation:</strong>
i=0: 0 mod 10 = 0 == nums[0].
i=1: 1 mod 10 = 1 == nums[1].
i=2: 2 mod 10 = 2 == nums[2].
Expand All @@ -40,7 +40,7 @@ All indices have i mod 10 == nums[i], so we return the smallest index 0.
<pre>
<strong>Input:</strong> nums = [4,3,2,1]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
<strong>Explanation:</strong>
i=0: 0 mod 10 = 0 != nums[0].
i=1: 1 mod 10 = 1 != nums[1].
i=2: 2 mod 10 = 2 == nums[2].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ encrypter.decrypt(&quot;eizfeiam&quot;); // return 2.
<li><code>1 &lt;= dictionary[i].length &lt;= 100</code></li>
<li>All <code>keys[i]</code> and <code>dictionary[i]</code> are <strong>unique</strong>.</li>
<li><code>1 &lt;= word1.length &lt;= 2000</code></li>
<li><code>1 &lt;= word2.length &lt;= 200</code></li>
<li><code>2 &lt;= word2.length &lt;= 200</code></li>
<li>All <code>word1[i]</code> appear in <code>keys</code>.</li>
<li><code>word2.length</code> is even.</li>
<li><code>keys</code>, <code>values[i]</code>, <code>dictionary[i]</code>, <code>word1</code>, and <code>word2</code> only contain lowercase English letters.</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3000-3099/3001.Mi
rating: 1796
source: 第 379 场周赛 Q2
tags:
- 数组
- 数学
- 枚举
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3000-3099/3001.Mi
rating: 1796
source: Weekly Contest 379 Q2
tags:
- Array
- Math
- Enumeration
---

Expand Down Expand Up @@ -55,7 +55,7 @@ It is impossible to capture the black queen in less than two moves since it is n
<pre>
<strong>Input:</strong> a = 5, b = 3, c = 3, d = 4, e = 5, f = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> We can capture the black queen in a single move by doing one of the following:
<strong>Explanation:</strong> We can capture the black queen in a single move by doing one of the following:
- Move the white rook to (5, 2).
- Move the white bishop to (5, 2).
</pre>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ tags:

<p>You are given a <em>positive</em> number <code>n</code>.</p>

<p>Return the <strong>smallest</strong> number <code>x</code> <strong>greater than</strong> or <strong>equal to</strong> <code>n</code>, such that the binary representation of <code>x</code> contains only <strong>set</strong> bits.</p>

<p>A <strong>set</strong> bit refers to a bit in the binary representation of a number that has a value of <code>1</code>.</p>
<p>Return the <strong>smallest</strong> number <code>x</code> <strong>greater than</strong> or <strong>equal to</strong> <code>n</code>, such that the binary representation of <code>x</code> contains only <span data-keyword="set-bit">set bits</span></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ tags:

<!-- description:start -->

<p>给你一个整数数组 <code>nums</code>。该数组包含 <code>n</code> 个元素,其中&nbsp;<strong>恰好&nbsp;</strong>有 <code>n - 2</code> 个元素是&nbsp;<strong>特殊数字&nbsp;</strong>。剩下的&nbsp;<strong>两个&nbsp;</strong>元素中,一个是这些&nbsp;<strong>特殊数字&nbsp;</strong>的 <strong>和</strong> ,另一个是&nbsp;<strong>异常值&nbsp;</strong>。</p>
<p>给你一个整数数组 <code>nums</code>。该数组包含 <code>n</code> 个元素,其中&nbsp;<strong>恰好&nbsp;</strong>有 <code>n - 2</code> 个元素是&nbsp;<strong>特殊数字&nbsp;</strong>。剩下的&nbsp;<strong>两个&nbsp;</strong>元素中,一个是所有&nbsp;<strong>特殊数字&nbsp;</strong>的 <strong>和</strong> ,另一个是&nbsp;<strong>异常值&nbsp;</strong>。</p>

<p><strong>异常值</strong> 的定义是:既不是原始特殊数字之一,也不是表示这些数字元素和的数字。</p>
<p><strong>异常值</strong> 的定义是:既不是原始特殊数字之一,也不是所有特殊数字的和。</p>

<p><strong>注意</strong>,特殊数字、和 以及 异常值 的下标必须&nbsp;<strong>不同&nbsp;</strong>,但可以共享&nbsp;<strong>相同</strong> 的值。</p>

Expand Down
Loading

0 comments on commit 2ee3b36

Please sign in to comment.