Skip to content

[pull] main from doocs:main #117

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 4 commits into from
Aug 3, 2025
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
4 changes: 2 additions & 2 deletions solution/0000-0099/0098.Validate Binary Search Tree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ tags:
<p><strong>有效</strong> 二叉搜索树定义如下:</p>

<ul>
<li>节点的左<span data-keyword="subtree">子树</span>只包含<strong> 小于 </strong>当前节点的数。</li>
<li>节点的右子树只包含 <strong>大于</strong> 当前节点的数。</li>
<li>节点的左<span data-keyword="subtree">子树</span>只包含<strong>&nbsp;严格小于 </strong>当前节点的数。</li>
<li>节点的右子树只包含 <strong>严格大于</strong> 当前节点的数。</li>
<li>所有左子树和右子树自身必须也是二叉搜索树。</li>
</ul>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ tags:
<p>A <strong>valid BST</strong> is defined as follows:</p>

<ul>
<li>The left <span data-keyword="subtree">subtree</span> of a node contains only nodes with keys <strong>less than</strong> the node&#39;s key.</li>
<li>The right subtree of a node contains only nodes with keys <strong>greater than</strong> the node&#39;s key.</li>
<li>The left <span data-keyword="subtree">subtree</span> of a node contains only nodes with keys&nbsp;<strong>strictly less than</strong> the node&#39;s key.</li>
<li>The right subtree of a node contains only nodes with keys <strong>strictly greater than</strong> the node&#39;s key.</li>
<li>Both the left and right subtrees must also be binary search trees.</li>
</ul>

Expand Down
2 changes: 1 addition & 1 deletion solution/0100-0199/0135.Candy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ tags:

<ul>
<li>每个孩子至少分配到 <code>1</code> 个糖果。</li>
<li>相邻两个孩子评分更高的孩子会获得更多的糖果。</li>
<li>相邻两个孩子中,评分更高的那个会获得更多的糖果。</li>
</ul>

<p>请你给每个孩子分发糖果,计算并返回需要准备的 <strong>最少糖果数目</strong> 。</p>
Expand Down
1 change: 1 addition & 0 deletions solution/0400-0499/0472.Concatenated Words/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ tags:
- 数组
- 字符串
- 动态规划
- 排序
---

<!-- problem:start -->
Expand Down
1 change: 1 addition & 0 deletions solution/0400-0499/0472.Concatenated Words/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ tags:
- Array
- String
- Dynamic Programming
- Sorting
---

<!-- problem:start -->
Expand Down
2 changes: 1 addition & 1 deletion solution/0400-0499/0499.The Maze III/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ tags:

<!-- description:start -->

<p>There is a ball in a <code>maze</code> with empty spaces (represented as <code>0</code>) and walls (represented as <code>1</code>). The ball can go through the empty spaces by rolling <strong>up, down, left or right</strong>, but it won&#39;t stop rolling until hitting a wall. When the ball stops, it could choose the next direction. There is also a hole in this maze. The ball will drop into the hole if it rolls onto the hole.</p>
<p>There is a ball in a <code>maze</code> with empty spaces (represented as <code>0</code>) and walls (represented as <code>1</code>). The ball can go through the empty spaces by rolling <strong>up, down, left or right</strong>, but it won&#39;t stop rolling until hitting a wall. When the ball stops, it could choose the next direction (must be different from last chosen direction). There is also a hole in this maze. The ball will drop into the hole if it rolls onto the hole.</p>

<p>Given the <code>m x n</code> <code>maze</code>, the ball&#39;s position <code>ball</code> and the hole&#39;s position <code>hole</code>, where <code>ball = [ball<sub>row</sub>, ball<sub>col</sub>]</code> and <code>hole = [hole<sub>row</sub>, hole<sub>col</sub>]</code>, return <em>a string </em><code>instructions</code><em> of all the instructions that the ball should follow to drop in the hole with the <strong>shortest distance</strong> possible</em>. If there are multiple valid instructions, return the <strong>lexicographically minimum</strong> one. If the ball can&#39;t drop in the hole, return <code>&quot;impossible&quot;</code>.</p>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ tags:
<pre>
<strong>Input:</strong> fruits = [[2,8],[6,3],[8,6]], startPos = 5, k = 4
<strong>Output:</strong> 9
<strong>Explanation:</strong>
<strong>Explanation:</strong>
The optimal way is to:
- Move right to position 6 and harvest 3 fruits
- Move right to position 8 and harvest 6 fruits
Expand All @@ -45,7 +45,7 @@ You moved 3 steps and harvested 3 + 6 = 9 fruits in total.
<pre>
<strong>Input:</strong> fruits = [[0,9],[4,1],[5,7],[6,2],[7,4],[10,9]], startPos = 5, k = 4
<strong>Output:</strong> 14
<strong>Explanation:</strong>
<strong>Explanation:</strong>
You can move at most k = 4 steps, so you cannot reach position 0 nor 10.
The optimal way is to:
- Harvest the 7 fruits at the starting position 5
Expand Down
6 changes: 3 additions & 3 deletions solution/2500-2599/2561.Rearranging Fruits/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ tags:
<p>You have two fruit baskets containing <code>n</code> fruits each. You are given two <strong>0-indexed</strong> integer arrays <code>basket1</code> and <code>basket2</code> representing the cost of fruit in each basket. You want to make both baskets <strong>equal</strong>. To do so, you can use the following operation as many times as you want:</p>

<ul>
<li>Chose two indices <code>i</code> and <code>j</code>, and swap the <code>i<font size="1">th</font>&nbsp;</code>fruit of <code>basket1</code> with the <code>j<font size="1">th</font></code>&nbsp;fruit of <code>basket2</code>.</li>
<li>The cost of the swap is <code>min(basket1[i],basket2[j])</code>.</li>
<li>Choose two indices <code>i</code> and <code>j</code>, and swap the <code>i<sup><font size="1">th</font></sup></code> fruit of <code>basket1</code> with the <code>j<sup><font size="1">th</font></sup></code> fruit of <code>basket2</code>.</li>
<li>The cost of the swap is <code>min(basket1[i], basket2[j])</code>.</li>
</ul>

<p>Two baskets are considered equal if sorting them according to the fruit cost makes them exactly the same baskets.</p>
Expand Down Expand Up @@ -55,7 +55,7 @@ tags:
<ul>
<li><code>basket1.length == basket2.length</code></li>
<li><code>1 &lt;= basket1.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= basket1[i],basket2[i]&nbsp;&lt;= 10<sup>9</sup></code></li>
<li><code>1 &lt;= basket1[i], basket2[i] &lt;= 10<sup>9</sup></code></li>
</ul>

<!-- description:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,32 +86,107 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3634.Mi

<!-- solution:start -->

### 方法一
### 方法一:排序 + 二分查找

我们首先对数组进行排序,然后我们从小到大枚举每个元素 $\textit{nums}[i]$ 作为平衡数组的最小值,那么平衡数组的最大值 $\textit{max}$ 必须满足 $\textit{max} \leq \textit{nums}[i] \times k$。因此,我们可以使用二分查找来找到第一个大于 $\textit{nums}[i] \times k$ 的元素的下标 $j$,那么此时平衡数组的长度为 $j - i$,我们记录下最大的长度 $\textit{cnt}$,最后的答案就是数组长度减去 $\textit{cnt}$。

时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是数组 $\textit{nums}$ 的长度。

<!-- tabs:start -->

#### Python3

```python

class Solution:
def minRemoval(self, nums: List[int], k: int) -> int:
nums.sort()
cnt = 0
for i, x in enumerate(nums):
j = bisect_right(nums, k * x)
cnt = max(cnt, j - i)
return len(nums) - cnt
```

#### Java

```java

class Solution {
public int minRemoval(int[] nums, int k) {
Arrays.sort(nums);
int cnt = 0;
int n = nums.length;
for (int i = 0; i < n; ++i) {
int j = n;
if (1L * nums[i] * k <= nums[n - 1]) {
j = Arrays.binarySearch(nums, nums[i] * k + 1);
j = j < 0 ? -j - 1 : j;
}
cnt = Math.max(cnt, j - i);
}
return n - cnt;
}
}
```

#### C++

```cpp

class Solution {
public:
int minRemoval(vector<int>& nums, int k) {
ranges::sort(nums);
int cnt = 0;
int n = nums.size();
for (int i = 0; i < n; ++i) {
int j = n;
if (1LL * nums[i] * k <= nums[n - 1]) {
j = upper_bound(nums.begin(), nums.end(), 1LL * nums[i] * k) - nums.begin();
}
cnt = max(cnt, j - i);
}
return n - cnt;
}
};
```

#### Go

```go
func minRemoval(nums []int, k int) int {
sort.Ints(nums)
n := len(nums)
cnt := 0
for i := 0; i < n; i++ {
j := n
if int64(nums[i])*int64(k) <= int64(nums[n-1]) {
target := int64(nums[i])*int64(k) + 1
j = sort.Search(n, func(x int) bool {
return int64(nums[x]) >= target
})
}
cnt = max(cnt, j-i)
}
return n - cnt
}
```

#### TypeScript

```ts
function minRemoval(nums: number[], k: number): number {
nums.sort((a, b) => a - b);
const n = nums.length;
let cnt = 0;
for (let i = 0; i < n; ++i) {
let j = n;
if (nums[i] * k <= nums[n - 1]) {
const target = nums[i] * k + 1;
j = _.sortedIndexBy(nums, target, x => x);
}
cnt = Math.max(cnt, j - i);
}
return n - cnt;
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,32 +84,107 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3634.Mi

<!-- solution:start -->

### Solution 1
### Solution 1: Sorting + Binary Search

We first sort the array, then enumerate each element $\textit{nums}[i]$ from small to large as the minimum value of the balanced array. The maximum value $\textit{max}$ of the balanced array must satisfy $\textit{max} \leq \textit{nums}[i] \times k$. Therefore, we can use binary search to find the index $j$ of the first element greater than $\textit{nums}[i] \times k$. At this point, the length of the balanced array is $j - i$. We record the maximum length $\textit{cnt}$, and the final answer is the array length minus $\textit{cnt}$.

The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$, where $n$ is the length of the array $\textit{nums}$.

<!-- tabs:start -->

#### Python3

```python

class Solution:
def minRemoval(self, nums: List[int], k: int) -> int:
nums.sort()
cnt = 0
for i, x in enumerate(nums):
j = bisect_right(nums, k * x)
cnt = max(cnt, j - i)
return len(nums) - cnt
```

#### Java

```java

class Solution {
public int minRemoval(int[] nums, int k) {
Arrays.sort(nums);
int cnt = 0;
int n = nums.length;
for (int i = 0; i < n; ++i) {
int j = n;
if (1L * nums[i] * k <= nums[n - 1]) {
j = Arrays.binarySearch(nums, nums[i] * k + 1);
j = j < 0 ? -j - 1 : j;
}
cnt = Math.max(cnt, j - i);
}
return n - cnt;
}
}
```

#### C++

```cpp

class Solution {
public:
int minRemoval(vector<int>& nums, int k) {
ranges::sort(nums);
int cnt = 0;
int n = nums.size();
for (int i = 0; i < n; ++i) {
int j = n;
if (1LL * nums[i] * k <= nums[n - 1]) {
j = upper_bound(nums.begin(), nums.end(), 1LL * nums[i] * k) - nums.begin();
}
cnt = max(cnt, j - i);
}
return n - cnt;
}
};
```

#### Go

```go
func minRemoval(nums []int, k int) int {
sort.Ints(nums)
n := len(nums)
cnt := 0
for i := 0; i < n; i++ {
j := n
if int64(nums[i])*int64(k) <= int64(nums[n-1]) {
target := int64(nums[i])*int64(k) + 1
j = sort.Search(n, func(x int) bool {
return int64(nums[x]) >= target
})
}
cnt = max(cnt, j-i)
}
return n - cnt
}
```

#### TypeScript

```ts
function minRemoval(nums: number[], k: number): number {
nums.sort((a, b) => a - b);
const n = nums.length;
let cnt = 0;
for (let i = 0; i < n; ++i) {
let j = n;
if (nums[i] * k <= nums[n - 1]) {
const target = nums[i] * k + 1;
j = _.sortedIndexBy(nums, target, x => x);
}
cnt = Math.max(cnt, j - i);
}
return n - cnt;
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public:
int minRemoval(vector<int>& nums, int k) {
ranges::sort(nums);
int cnt = 0;
int n = nums.size();
for (int i = 0; i < n; ++i) {
int j = n;
if (1LL * nums[i] * k <= nums[n - 1]) {
j = upper_bound(nums.begin(), nums.end(), 1LL * nums[i] * k) - nums.begin();
}
cnt = max(cnt, j - i);
}
return n - cnt;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
func minRemoval(nums []int, k int) int {
sort.Ints(nums)
n := len(nums)
cnt := 0
for i := 0; i < n; i++ {
j := n
if int64(nums[i])*int64(k) <= int64(nums[n-1]) {
target := int64(nums[i])*int64(k) + 1
j = sort.Search(n, func(x int) bool {
return int64(nums[x]) >= target
})
}
cnt = max(cnt, j-i)
}
return n - cnt
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public int minRemoval(int[] nums, int k) {
Arrays.sort(nums);
int cnt = 0;
int n = nums.length;
for (int i = 0; i < n; ++i) {
int j = n;
if (1L * nums[i] * k <= nums[n - 1]) {
j = Arrays.binarySearch(nums, nums[i] * k + 1);
j = j < 0 ? -j - 1 : j;
}
cnt = Math.max(cnt, j - i);
}
return n - cnt;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Solution:
def minRemoval(self, nums: List[int], k: int) -> int:
nums.sort()
cnt = 0
for i, x in enumerate(nums):
j = bisect_right(nums, k * x)
cnt = max(cnt, j - i)
return len(nums) - cnt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function minRemoval(nums: number[], k: number): number {
nums.sort((a, b) => a - b);
const n = nums.length;
let cnt = 0;
for (let i = 0; i < n; ++i) {
let j = n;
if (nums[i] * k <= nums[n - 1]) {
const target = nums[i] * k + 1;
j = _.sortedIndexBy(nums, target, x => x);
}
cnt = Math.max(cnt, j - i);
}
return n - cnt;
}
Loading
Loading