Skip to content
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

feat: add solutions to lc problems: No.34,1870,1894 #2140

Merged
merged 1 commit into from
Dec 22, 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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@

- [在排序数组中查找元素的第一个和最后一个位置](/solution/0000-0099/0034.Find%20First%20and%20Last%20Position%20of%20Element%20in%20Sorted%20Array/README.md) - `二分查找`
- [准时到达的列车最小时速](/solution/1800-1899/1870.Minimum%20Speed%20to%20Arrive%20on%20Time/README.md) - `二分查找`
- [找到需要补充粉笔的学生编号](/solution/1800-1899/1894.Find%20the%20Student%20that%20Will%20Replace%20the%20Chalk/README.md) - `二分查找`
- [可移除字符的最大数目](/solution/1800-1899/1898.Maximum%20Number%20of%20Removable%20Characters/README.md) - `二分查找`
- [排序数组](/solution/0900-0999/0912.Sort%20an%20Array/README.md) - `快速排序`、`归并排序`
- [字符串相加](/solution/0400-0499/0415.Add%20Strings/README.md) - `高精度加法`
Expand Down
1 change: 0 additions & 1 deletion README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ The repository is maintained by the Doocs community, and please give us a [star]

- [Find First and Last Position of Element in Sorted Array](/solution/0000-0099/0034.Find%20First%20and%20Last%20Position%20of%20Element%20in%20Sorted%20Array/README_EN.md) - `Binary search`
- [Minimum Speed to Arrive on Time](/solution/1800-1899/1870.Minimum%20Speed%20to%20Arrive%20on%20Time/README_EN.md) - `Binary search`
- [Find the Student that Will Replace the Chalk](/solution/1800-1899/1894.Find%20the%20Student%20that%20Will%20Replace%20the%20Chalk/README_EN.md) - `Binary search`
- [Maximum Number of Removable Characters](/solution/1800-1899/1898.Maximum%20Number%20of%20Removable%20Characters/README_EN.md) - `Binary search`
- [Sort an Array](/solution/0900-0999/0912.Sort%20an%20Array/README_EN.md) - `Quick Sort`, `Merge Sort`
- [Add Strings](/solution/0400-0499/0415.Add%20Strings/README_EN.md) - `Addition of large numbers`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,16 @@ int search(int left, int right) {
}
```

做二分题目时,可以按照以下步骤
做二分题目时,可以按照以下套路

1. 写出循环条件:`while (left < right)`,注意是 `left < right`,而非 `left <= right`
1. 循环体内,先无脑写出 `mid = (left + right) >> 1`
1. 根据具体题目,实现 `check()` 函数(有时很简单的逻辑,可以不定义 `check`),想一下究竟要用 `right = mid`(模板 1) 还是 `left = mid`(模板 2);
- 如果 `right = mid`,那么无脑写出 else 语句 `left = mid + 1`,并且不需要更改 mid 的计算,即保持 `mid = (left + right) >> 1`
- 如果 `left = mid`,那么无脑写出 else 语句 `right = mid - 1`,并且在 mid 计算时补充 +1,即 `mid = (left + right + 1) >> 1`。
1. 循环结束时,left 与 right 相等。
1. 写出循环条件 $left < right$
1. 循环体内,不妨先写 $mid = \lfloor \frac{left + right}{2} \rfloor$
1. 根据具体题目,实现 $check()$ 函数(有时很简单的逻辑,可以不定义 $check$),想一下究竟要用 $right = mid$(模板 $1$) 还是 $left = mid$(模板 $2$);
    - 如果 $right = mid$,那么写出 else 语句 $left = mid + 1$,并且不需要更改 mid 的计算,即保持 $mid = \lfloor \frac{left + right}{2} \rfloor$
    - 如果 $left = mid$,那么写出 else 语句 $right = mid - 1$,并且在 $mid$ 计算时补充 +1,即 $mid = \lfloor \frac{left + right + 1}{2} \rfloor$;
1. 循环结束时,$left$$right$ 相等。

注意,这两个模板的优点是始终保持答案位于二分区间内,二分结束条件对应的值恰好在答案所处的位置。 对于可能无解的情况,只要判断二分结束后的 left 或者 right 是否满足题意即可。
注意,这两个模板的优点是始终保持答案位于二分区间内,二分结束条件对应的值恰好在答案所处的位置。 对于可能无解的情况,只要判断二分结束后的 $left$ 或者 $right$ 是否满足题意即可。

<!-- tabs:start -->

Expand Down Expand Up @@ -159,34 +159,6 @@ public:
};
```

### **JavaScript**

```js
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var searchRange = function (nums, target) {
function search(x) {
let left = 0,
right = nums.length;
while (left < right) {
const mid = (left + right) >> 1;
if (nums[mid] >= x) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
const l = search(target);
const r = search(target + 1);
return l == r ? [-1, -1] : [l, r - 1];
};
```

### **Go**

```go
Expand Down Expand Up @@ -233,6 +205,33 @@ impl Solution {

```ts
function searchRange(nums: number[], target: number): number[] {
const search = (x: number): number => {
let [left, right] = [0, nums.length];
while (left < right) {
const mid = (left + right) >> 1;
if (nums[mid] >= x) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
};
const l = search(target);
const r = search(target + 1);
return l === r ? [-1, -1] : [l, r - 1];
}
```

### **JavaScript**

```js
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var searchRange = function (nums, target) {
function search(x) {
let left = 0,
right = nums.length;
Expand All @@ -249,7 +248,7 @@ function searchRange(nums: number[], target: number): number[] {
const l = search(target);
const r = search(target + 1);
return l == r ? [-1, -1] : [l, r - 1];
}
};
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ int search(int left, int right) {
}
```

When doing binary search problems, you can follow the following routine:

1. Write out the loop condition $left < right$;
2. Inside the loop, you might as well write $mid = \lfloor \frac{left + right}{2} \rfloor$ first;
3. According to the specific problem, implement the $check()$ function (sometimes the logic is very simple, you can not define $check$), think about whether to use $right = mid$ (Template $1$) or $left = mid$ (Template $2$);
- If $right = mid$, then write the else statement $left = mid + 1$, and there is no need to change the calculation of $mid$, that is, keep $mid = \lfloor \frac{left + right}{2} \rfloor$;
- If $left = mid$, then write the else statement $right = mid - 1$, and add +1 when calculating $mid$, that is, $mid = \lfloor \frac{left + right + 1}{2} \rfloor$;
4. When the loop ends, $left$ equals $right$.

Note that the advantage of these two templates is that they always keep the answer within the binary search interval, and the value corresponding to the end condition of the binary search is exactly at the position of the answer. For the case that may have no solution, just check whether the $left$ or $right$ after the binary search ends satisfies the problem.

<!-- tabs:start -->

### **Python3**
Expand Down Expand Up @@ -130,34 +141,6 @@ public:
};
```

### **JavaScript**

```js
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var searchRange = function (nums, target) {
function search(x) {
let left = 0,
right = nums.length;
while (left < right) {
const mid = (left + right) >> 1;
if (nums[mid] >= x) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
const l = search(target);
const r = search(target + 1);
return l == r ? [-1, -1] : [l, r - 1];
};
```

### **Go**

```go
Expand Down Expand Up @@ -204,6 +187,33 @@ impl Solution {

```ts
function searchRange(nums: number[], target: number): number[] {
const search = (x: number): number => {
let [left, right] = [0, nums.length];
while (left < right) {
const mid = (left + right) >> 1;
if (nums[mid] >= x) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
};
const l = search(target);
const r = search(target + 1);
return l === r ? [-1, -1] : [l, r - 1];
}
```

### **JavaScript**

```js
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var searchRange = function (nums, target) {
function search(x) {
let left = 0,
right = nums.length;
Expand All @@ -220,7 +230,7 @@ function searchRange(nums: number[], target: number): number[] {
const l = search(target);
const r = search(target + 1);
return l == r ? [-1, -1] : [l, r - 1];
}
};
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
function searchRange(nums: number[], target: number): number[] {
function search(x) {
let left = 0,
right = nums.length;
const search = (x: number): number => {
let [left, right] = [0, nums.length];
while (left < right) {
const mid = (left + right) >> 1;
if (nums[mid] >= x) {
Expand All @@ -11,8 +10,8 @@ function searchRange(nums: number[], target: number): number[] {
}
}
return left;
}
};
const l = search(target);
const r = search(target + 1);
return l == r ? [-1, -1] : [l, r - 1];
return l === r ? [-1, -1] : [l, r - 1];
}
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,9 @@ function minimumMountainRemovals(nums: number[]): number {
}
```

### **TypeScript**
### **Rust**

```ts
```rust
impl Solution {
pub fn minimum_mountain_removals(nums: Vec<i32>) -> i32 {
let n = nums.len();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ function minimumMountainRemovals(nums: number[]): number {
}
```

### **TypeScript**
### **Rust**

```ts
```rust
impl Solution {
pub fn minimum_mountain_removals(nums: Vec<i32>) -> i32 {
let n = nums.len();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,16 @@ int search(int left, int right) {
}
```

做二分题目时,可以按照以下步骤
做二分题目时,可以按照以下套路

1. 写出循环条件:`while (left < right)`,注意是 `left < right`,而非 `left <= right`
1. 循环体内,先无脑写出 `mid = (left + right) >> 1`
1. 根据具体题目,实现 `check()` 函数(有时很简单的逻辑,可以不定义 `check`),想一下究竟要用 `right = mid`(模板 1) 还是 `left = mid`(模板 2);
- 如果 `right = mid`,那么无脑写出 else 语句 `left = mid + 1`,并且不需要更改 mid 的计算,即保持 `mid = (left + right) >> 1`
- 如果 `left = mid`,那么无脑写出 else 语句 `right = mid - 1`,并且在 mid 计算时补充 +1,即 `mid = (left + right + 1) >> 1`。
1. 循环结束时,left 与 right 相等。
1. 写出循环条件 $left < right$
1. 循环体内,不妨先写 $mid = \lfloor \frac{left + right}{2} \rfloor$
1. 根据具体题目,实现 $check()$ 函数(有时很简单的逻辑,可以不定义 $check$),想一下究竟要用 $right = mid$(模板 $1$) 还是 $left = mid$(模板 $2$);
    - 如果 $right = mid$,那么写出 else 语句 $left = mid + 1$,并且不需要更改 mid 的计算,即保持 $mid = \lfloor \frac{left + right}{2} \rfloor$
    - 如果 $left = mid$,那么写出 else 语句 $right = mid - 1$,并且在 $mid$ 计算时补充 +1,即 $mid = \lfloor \frac{left + right + 1}{2} \rfloor$;
1. 循环结束时,$left$$right$ 相等。

注意,这两个模板的优点是始终保持答案位于二分区间内,二分结束条件对应的值恰好在答案所处的位置。 对于可能无解的情况,只要判断二分结束后的 left 或者 right 是否满足题意即可。
注意,这两个模板的优点是始终保持答案位于二分区间内,二分结束条件对应的值恰好在答案所处的位置。 对于可能无解的情况,只要判断二分结束后的 $left$ 或者 $right$ 是否满足题意即可。

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,17 @@ int search(int left, int right) {
}
```

When doing binary search problems, you can follow the following routine:

1. Write out the loop condition $left < right$;
2. Inside the loop, you might as well write $mid = \lfloor \frac{left + right}{2} \rfloor$ first;
3. According to the specific problem, implement the $check()$ function (sometimes the logic is very simple, you can not define $check$), think about whether to use $right = mid$ (Template $1$) or $left = mid$ (Template $2$);
- If $right = mid$, then write the else statement $left = mid + 1$, and there is no need to change the calculation of $mid$, that is, keep $mid = \lfloor \frac{left + right}{2} \rfloor$;
- If $left = mid$, then write the else statement $right = mid - 1$, and add +1 when calculating $mid$, that is, $mid = \lfloor \frac{left + right + 1}{2} \rfloor$;
4. When the loop ends, $left$ equals $right$.

Note that the advantage of these two templates is that they always keep the answer within the binary search interval, and the value corresponding to the end condition of the binary search is exactly at the position of the answer. For the case that may have no solution, just check whether the $left$ or $right$ after the binary search ends satisfies the problem.

<!-- tabs:start -->

### **Python3**
Expand Down
Loading