Skip to content

feat: add solutions to lc problem: No.2599 #950

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
Mar 24, 2023
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
2 changes: 1 addition & 1 deletion solution/0200-0299/0263.Ugly Number/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<pre>
<strong>输入:</strong>n = 14
<strong>输出:</strong>false
<strong>解释:</strong>14 不是丑数,因为它包含了另外一个质因数 7
<strong>解释:</strong>14 不是丑数,因为它包含了另外一个质因数&nbsp;<code>7 </code>
</pre>

<p>&nbsp;</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class Solution {
return -1;
}
}
```
```

### **...**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class Solution {
return -1;
}
}
```
```

### **...**

Expand Down
2 changes: 1 addition & 1 deletion solution/0600-0699/0605.Can Place Flowers/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<p>You have a long flowerbed in which some of the plots are planted, and some are not. However, flowers cannot be planted in <strong>adjacent</strong> plots.</p>

<p>Given an integer array <code>flowerbed</code> containing <code>0</code>&#39;s and <code>1</code>&#39;s, where <code>0</code> means empty and <code>1</code> means not empty, and an integer <code>n</code>, return <em>if</em> <code>n</code> new flowers can be planted in the <code>flowerbed</code> without violating the no-adjacent-flowers rule.</p>
<p>Given an integer array <code>flowerbed</code> containing <code>0</code>&#39;s and <code>1</code>&#39;s, where <code>0</code> means empty and <code>1</code> means not empty, and an integer <code>n</code>, return <code>true</code>&nbsp;<em>if</em> <code>n</code> <em>new flowers can be planted in the</em> <code>flowerbed</code> <em>without violating the no-adjacent-flowers rule and</em> <code>false</code> <em>otherwise</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
Expand Down
2 changes: 1 addition & 1 deletion solution/0600-0699/0665.Non-decreasing Array/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class Solution {
}
return true;
}

private boolean isSorted(int[] nums) {
for (int i = 0; i < nums.length - 1; ++i) {
if (nums[i] > nums[i + 1]) {
Expand Down
4 changes: 2 additions & 2 deletions solution/1400-1499/1406.Stone Game III/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class Solution {
private int n;
private int[] s;
private Integer[] f;

public String stoneGameIII(int[] stoneValue) {
n = stoneValue.length;
s = new int[n + 1];
Expand All @@ -95,7 +95,7 @@ class Solution {
int b = s[n] - a;
return a == b ? "Tie" : a > b ? "Alice" : "Bob";
}

private int dfs(int i) {
if (i >= n) {
return 0;
Expand Down
8 changes: 4 additions & 4 deletions solution/1600-1699/1630.Arithmetic Subarrays/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@

函数 $check(nums, l, r)$ 的实现逻辑如下:

- 首先,我们计算子数组的长度 $n = r - l + 1$,并将子数组中的元素放入集合 $s$ 中,方便后续的查找;
- 然后,我们获取子数组中的最小值 $a_1$ 和最大值 $a_n$,如果 $a_n - a_1$ 不能被 $n - 1$ 整除,那么子数组不可能形成等差数列,直接返回 $false$;否则,我们计算等差数列的公差 $d = \frac{a_n - a_1}{n - 1}$;
- 接下来从 $a_1$ 开始,依次计算等差数列中第 $i$ 项元素,如果第 $i$ 项元素 $a_1 + (i - 1) \times d$ 不在集合 $s$ 中,那么子数组不可能形成等差数列,直接返回 $false$;否则,当我们遍历完所有的元素,说明子数组可以重新排列形成等差数列,返回 $true$。
- 首先,我们计算子数组的长度 $n = r - l + 1$,并将子数组中的元素放入集合 $s$ 中,方便后续的查找;
- 然后,我们获取子数组中的最小值 $a_1$ 和最大值 $a_n$,如果 $a_n - a_1$ 不能被 $n - 1$ 整除,那么子数组不可能形成等差数列,直接返回 $false$;否则,我们计算等差数列的公差 $d = \frac{a_n - a_1}{n - 1}$;
- 接下来从 $a_1$ 开始,依次计算等差数列中第 $i$ 项元素,如果第 $i$ 项元素 $a_1 + (i - 1) \times d$ 不在集合 $s$ 中,那么子数组不可能形成等差数列,直接返回 $false$;否则,当我们遍历完所有的元素,说明子数组可以重新排列形成等差数列,返回 $true$。

在主函数中,我们遍历所有的查询,对于每个查询 $l[i]$ 和 $r[i]$,我们调用函数 $check(nums, l[i], r[i])$ 判断子数组是否可以重新排列形成等差数列,将结果存入答案数组中。

Expand All @@ -86,7 +86,7 @@ class Solution:
a1, an = min(nums[l: l + n]), max(nums[l: l + n])
d, mod = divmod(an - a1, n - 1)
return mod == 0 and all((a1 + (i - 1) * d) in s for i in range(1, n))

return [check(nums, left, right) for left, right in zip(l, r)]
```

Expand Down
2 changes: 1 addition & 1 deletion solution/1600-1699/1630.Arithmetic Subarrays/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Solution:
a1, an = min(nums[l: l + n]), max(nums[l: l + n])
d, mod = divmod(an - a1, n - 1)
return mod == 0 and all((a1 + (i - 1) * d) in s for i in range(1, n))

return [check(nums, left, right) for left, right in zip(l, r)]
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

<!-- 这里写题目描述 -->

<p>给你两个字符串 <code>s</code> 和 <code>t</code> ,请你找出 <code>s</code> 中的非空子串的数目,这些子串满足替换 <strong>一个不同字符</strong> 以后,是 <code>t</code> 串的子串。换言之,请你找到 <code>s</code> 和 <code>t</code> 串中 <strong>恰好</strong> 只有一个字符不同的子字符串对的数目。</p>
<p>给你两个字符串&nbsp;<code>s</code> 和&nbsp;<code>t</code>&nbsp;,请你找出 <code>s</code>&nbsp;中的非空子串的数目,这些子串满足替换 <strong>一个不同字符</strong>&nbsp;以后,是 <code>t</code>&nbsp;串的子串。换言之,请你找到 <code>s</code>&nbsp;和 <code>t</code>&nbsp;串中 <strong>恰好</strong>&nbsp;只有一个字符不同的子字符串对的数目。</p>

<p>比方说, <code>"<strong>compute</strong>r"</code> 和 <code>"<strong>computa</strong>tion"</code> 加粗部分只有一个字符不同: <code>'e'</code>/<code>'a'</code> ,所以这一对子字符串会给答案加 1 。</p>
<p>比方说,&nbsp;<code>"<u>compute</u>r"</code>&nbsp;and&nbsp;<code>"<u>computa</u>tion"&nbsp;</code>只有一个字符不同:&nbsp;<code>'e'</code>/<code>'a'</code>&nbsp;,所以这一对子字符串会给答案加 1 。</p>

<p>请你返回满足上述条件的不同子字符串对数目。</p>

<p>一个 <strong>子字符串</strong> 是一个字符串中连续的字符。</p>
<p>一个 <strong>子字符串</strong>&nbsp;是一个字符串中连续的字符。</p>

<p> </p>
<p>&nbsp;</p>

<p><strong>示例 1:</strong></p>

Expand Down Expand Up @@ -57,13 +57,13 @@
<b>输出:</b>10
</pre>

<p> </p>
<p>&nbsp;</p>

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

<ul>
<li><code>1 <= s.length, t.length <= 100</code></li>
<li><code>s</code> 和 <code>t</code> 都只包含小写英文字母。</li>
<li><code>1 &lt;= s.length, t.length &lt;= 100</code></li>
<li><code>s</code> 和&nbsp;<code>t</code>&nbsp;都只包含小写英文字母。</li>
</ul>

## 解法
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@

<ul>
<li>Take any bag of balls and divide it into two new bags with a <strong>positive </strong>number of balls.

<ul>
<li>For example, a bag of <code>5</code> balls can become two new bags of <code>1</code> and <code>4</code> balls, or two new bags of <code>2</code> and <code>3</code> balls.</li>
</ul>
</li>

</ul>

<p>Your penalty is the <strong>maximum</strong> number of balls in a bag. You want to <strong>minimize</strong> your penalty after the operations.</p>
Expand Down
17 changes: 10 additions & 7 deletions solution/1900-1999/1943.Describe the Painting/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,19 @@
<p>&nbsp;</p>

<p><strong>示例 1:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1900-1999/1943.Describe%20the%20Painting/images/1.png" style="width: 529px; height: 241px;">
<pre><b>输入:</b>segments = [[1,4,5],[4,7,7],[1,7,9]]
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1900-1999/1943.Describe%20the%20Painting/images/1.png" style="width: 529px; height: 241px;" />
<pre>
<b>输入:</b>segments = [[1,4,5],[4,7,7],[1,7,9]]
<b>输出:</b>[[1,4,14],[4,7,16]]
<strong>解释:</strong>绘画借故偶可以表示为
<strong>解释:</strong>绘画结果可以表示为
- [1,4) 颜色为 {5,9} (和为 14),分别来自第一和第二个线段。
- [4,7) 颜色为 {7,9} (和为 16),分别来自第二和第三个线段。
</pre>

<p><strong>示例 2:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1900-1999/1943.Describe%20the%20Painting/images/2.png" style="width: 532px; height: 219px;">
<pre><b>输入:</b>segments = [[1,7,9],[6,8,15],[8,10,7]]
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1900-1999/1943.Describe%20the%20Painting/images/2.png" style="width: 532px; height: 219px;" />
<pre>
<b>输入:</b>segments = [[1,7,9],[6,8,15],[8,10,7]]
<b>输出:</b>[[1,6,9],[6,7,24],[7,8,15],[8,10,7]]
<b>解释:</b>绘画结果可以以表示为:
- [1,6) 颜色为 9 ,来自第一个线段。
Expand All @@ -56,8 +58,9 @@
</pre>

<p><strong>示例 3:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1900-1999/1943.Describe%20the%20Painting/images/c1.png" style="width: 529px; height: 289px;">
<pre><b>输入:</b>segments = [[1,4,5],[1,4,7],[4,7,1],[4,7,11]]
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1900-1999/1943.Describe%20the%20Painting/images/c1.png" style="width: 529px; height: 289px;" />
<pre>
<b>输入:</b>segments = [[1,4,5],[1,4,7],[4,7,1],[4,7,11]]
<b>输出:</b>[[1,4,12],[4,7,12]]
<strong>解释:</strong>绘画结果可以表示为:
- [1,4) 颜色为 {5,7} (和为 12),分别来自第一和第二个线段。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

## Description

<p>You are given a <strong>0-indexed</strong> 2D integer array <code>flowers</code>, where <code>flowers[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means the <code>i<sup>th</sup></code> flower will be in <strong>full bloom</strong> from <code>start<sub>i</sub></code> to <code>end<sub>i</sub></code> (<strong>inclusive</strong>). You are also given a <strong>0-indexed</strong> integer array <code>persons</code> of size <code>n</code>, where <code>persons[i]</code> is the time that the <code>i<sup>th</sup></code> person will arrive to see the flowers.</p>
<p>You are given a <strong>0-indexed</strong> 2D integer array <code>flowers</code>, where <code>flowers[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means the <code>i<sup>th</sup></code> flower will be in <strong>full bloom</strong> from <code>start<sub>i</sub></code> to <code>end<sub>i</sub></code> (<strong>inclusive</strong>). You are also given a <strong>0-indexed</strong> integer array <code>people</code> of size <code>n</code>, where <code>poeple[i]</code> is the time that the <code>i<sup>th</sup></code> person will arrive to see the flowers.</p>

<p>Return <em>an integer array </em><code>answer</code><em> of size </em><code>n</code><em>, where </em><code>answer[i]</code><em> is the <strong>number</strong> of flowers that are in full bloom when the </em><code>i<sup>th</sup></code><em> person arrives.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2200-2299/2251.Number%20of%20Flowers%20in%20Full%20Bloom/images/ex1new.jpg" style="width: 550px; height: 216px;" />
<pre>
<strong>Input:</strong> flowers = [[1,6],[3,7],[9,12],[4,13]], persons = [2,3,7,11]
<strong>Input:</strong> flowers = [[1,6],[3,7],[9,12],[4,13]], poeple = [2,3,7,11]
<strong>Output:</strong> [1,2,2,2]
<strong>Explanation: </strong>The figure above shows the times when the flowers are in full bloom and when the people arrive.
For each person, we return the number of flowers in full bloom during their arrival.
Expand All @@ -21,7 +21,7 @@ For each person, we return the number of flowers in full bloom during their arri
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2200-2299/2251.Number%20of%20Flowers%20in%20Full%20Bloom/images/ex2new.jpg" style="width: 450px; height: 195px;" />
<pre>
<strong>Input:</strong> flowers = [[1,10],[3,3]], persons = [3,3,2]
<strong>Input:</strong> flowers = [[1,10],[3,3]], poeple = [3,3,2]
<strong>Output:</strong> [2,2,1]
<strong>Explanation:</strong> The figure above shows the times when the flowers are in full bloom and when the people arrive.
For each person, we return the number of flowers in full bloom during their arrival.
Expand All @@ -34,8 +34,8 @@ For each person, we return the number of flowers in full bloom during their arri
<li><code>1 &lt;= flowers.length &lt;= 5 * 10<sup>4</sup></code></li>
<li><code>flowers[i].length == 2</code></li>
<li><code>1 &lt;= start<sub>i</sub> &lt;= end<sub>i</sub> &lt;= 10<sup>9</sup></code></li>
<li><code>1 &lt;= persons.length &lt;= 5 * 10<sup>4</sup></code></li>
<li><code>1 &lt;= persons[i] &lt;= 10<sup>9</sup></code></li>
<li><code>1 &lt;= people.length &lt;= 5 * 10<sup>4</sup></code></li>
<li><code>1 &lt;= people[i] &lt;= 10<sup>9</sup></code></li>
</ul>

## Solutions
Expand Down
Loading