Skip to content

Commit 1e1b672

Browse files
authored
feat: add solutions to lc problems: No.34,1870,1894 (doocs#2140)
1 parent 0e3db4b commit 1e1b672

File tree

18 files changed

+302
-264
lines changed

18 files changed

+302
-264
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242

4343
- [在排序数组中查找元素的第一个和最后一个位置](/solution/0000-0099/0034.Find%20First%20and%20Last%20Position%20of%20Element%20in%20Sorted%20Array/README.md) - `二分查找`
4444
- [准时到达的列车最小时速](/solution/1800-1899/1870.Minimum%20Speed%20to%20Arrive%20on%20Time/README.md) - `二分查找`
45-
- [找到需要补充粉笔的学生编号](/solution/1800-1899/1894.Find%20the%20Student%20that%20Will%20Replace%20the%20Chalk/README.md) - `二分查找`
4645
- [可移除字符的最大数目](/solution/1800-1899/1898.Maximum%20Number%20of%20Removable%20Characters/README.md) - `二分查找`
4746
- [排序数组](/solution/0900-0999/0912.Sort%20an%20Array/README.md) - `快速排序``归并排序`
4847
- [字符串相加](/solution/0400-0499/0415.Add%20Strings/README.md) - `高精度加法`

README_EN.md

-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ The repository is maintained by the Doocs community, and please give us a [star]
4343

4444
- [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`
4545
- [Minimum Speed to Arrive on Time](/solution/1800-1899/1870.Minimum%20Speed%20to%20Arrive%20on%20Time/README_EN.md) - `Binary search`
46-
- [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`
4746
- [Maximum Number of Removable Characters](/solution/1800-1899/1898.Maximum%20Number%20of%20Removable%20Characters/README_EN.md) - `Binary search`
4847
- [Sort an Array](/solution/0900-0999/0912.Sort%20an%20Array/README_EN.md) - `Quick Sort`, `Merge Sort`
4948
- [Add Strings](/solution/0400-0499/0415.Add%20Strings/README_EN.md) - `Addition of large numbers`

solution/0000-0099/0034.Find First and Last Position of Element in Sorted Array/README.md

+36-37
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,16 @@ int search(int left, int right) {
9393
}
9494
```
9595

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

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

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

107107
<!-- tabs:start -->
108108

@@ -159,34 +159,6 @@ public:
159159
};
160160
```
161161
162-
### **JavaScript**
163-
164-
```js
165-
/**
166-
* @param {number[]} nums
167-
* @param {number} target
168-
* @return {number[]}
169-
*/
170-
var searchRange = function (nums, target) {
171-
function search(x) {
172-
let left = 0,
173-
right = nums.length;
174-
while (left < right) {
175-
const mid = (left + right) >> 1;
176-
if (nums[mid] >= x) {
177-
right = mid;
178-
} else {
179-
left = mid + 1;
180-
}
181-
}
182-
return left;
183-
}
184-
const l = search(target);
185-
const r = search(target + 1);
186-
return l == r ? [-1, -1] : [l, r - 1];
187-
};
188-
```
189-
190162
### **Go**
191163
192164
```go
@@ -233,6 +205,33 @@ impl Solution {
233205

234206
```ts
235207
function searchRange(nums: number[], target: number): number[] {
208+
const search = (x: number): number => {
209+
let [left, right] = [0, nums.length];
210+
while (left < right) {
211+
const mid = (left + right) >> 1;
212+
if (nums[mid] >= x) {
213+
right = mid;
214+
} else {
215+
left = mid + 1;
216+
}
217+
}
218+
return left;
219+
};
220+
const l = search(target);
221+
const r = search(target + 1);
222+
return l === r ? [-1, -1] : [l, r - 1];
223+
}
224+
```
225+
226+
### **JavaScript**
227+
228+
```js
229+
/**
230+
* @param {number[]} nums
231+
* @param {number} target
232+
* @return {number[]}
233+
*/
234+
var searchRange = function (nums, target) {
236235
function search(x) {
237236
let left = 0,
238237
right = nums.length;
@@ -249,7 +248,7 @@ function searchRange(nums: number[], target: number): number[] {
249248
const l = search(target);
250249
const r = search(target + 1);
251250
return l == r ? [-1, -1] : [l, r - 1];
252-
}
251+
};
253252
```
254253

255254
### **...**

solution/0000-0099/0034.Find First and Last Position of Element in Sorted Array/README_EN.md

+39-29
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,17 @@ int search(int left, int right) {
7979
}
8080
```
8181

82+
When doing binary search problems, you can follow the following routine:
83+
84+
1. Write out the loop condition $left < right$;
85+
2. Inside the loop, you might as well write $mid = \lfloor \frac{left + right}{2} \rfloor$ first;
86+
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$);
87+
- 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$;
88+
- 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$;
89+
4. When the loop ends, $left$ equals $right$.
90+
91+
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.
92+
8293
<!-- tabs:start -->
8394

8495
### **Python3**
@@ -130,34 +141,6 @@ public:
130141
};
131142
```
132143
133-
### **JavaScript**
134-
135-
```js
136-
/**
137-
* @param {number[]} nums
138-
* @param {number} target
139-
* @return {number[]}
140-
*/
141-
var searchRange = function (nums, target) {
142-
function search(x) {
143-
let left = 0,
144-
right = nums.length;
145-
while (left < right) {
146-
const mid = (left + right) >> 1;
147-
if (nums[mid] >= x) {
148-
right = mid;
149-
} else {
150-
left = mid + 1;
151-
}
152-
}
153-
return left;
154-
}
155-
const l = search(target);
156-
const r = search(target + 1);
157-
return l == r ? [-1, -1] : [l, r - 1];
158-
};
159-
```
160-
161144
### **Go**
162145
163146
```go
@@ -204,6 +187,33 @@ impl Solution {
204187

205188
```ts
206189
function searchRange(nums: number[], target: number): number[] {
190+
const search = (x: number): number => {
191+
let [left, right] = [0, nums.length];
192+
while (left < right) {
193+
const mid = (left + right) >> 1;
194+
if (nums[mid] >= x) {
195+
right = mid;
196+
} else {
197+
left = mid + 1;
198+
}
199+
}
200+
return left;
201+
};
202+
const l = search(target);
203+
const r = search(target + 1);
204+
return l === r ? [-1, -1] : [l, r - 1];
205+
}
206+
```
207+
208+
### **JavaScript**
209+
210+
```js
211+
/**
212+
* @param {number[]} nums
213+
* @param {number} target
214+
* @return {number[]}
215+
*/
216+
var searchRange = function (nums, target) {
207217
function search(x) {
208218
let left = 0,
209219
right = nums.length;
@@ -220,7 +230,7 @@ function searchRange(nums: number[], target: number): number[] {
220230
const l = search(target);
221231
const r = search(target + 1);
222232
return l == r ? [-1, -1] : [l, r - 1];
223-
}
233+
};
224234
```
225235

226236
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
function searchRange(nums: number[], target: number): number[] {
2-
function search(x) {
3-
let left = 0,
4-
right = nums.length;
2+
const search = (x: number): number => {
3+
let [left, right] = [0, nums.length];
54
while (left < right) {
65
const mid = (left + right) >> 1;
76
if (nums[mid] >= x) {
@@ -11,8 +10,8 @@ function searchRange(nums: number[], target: number): number[] {
1110
}
1211
}
1312
return left;
14-
}
13+
};
1514
const l = search(target);
1615
const r = search(target + 1);
17-
return l == r ? [-1, -1] : [l, r - 1];
16+
return l === r ? [-1, -1] : [l, r - 1];
1817
}

solution/1600-1699/1671.Minimum Number of Removals to Make Mountain Array/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,9 @@ function minimumMountainRemovals(nums: number[]): number {
219219
}
220220
```
221221

222-
### **TypeScript**
222+
### **Rust**
223223

224-
```ts
224+
```rust
225225
impl Solution {
226226
pub fn minimum_mountain_removals(nums: Vec<i32>) -> i32 {
227227
let n = nums.len();

solution/1600-1699/1671.Minimum Number of Removals to Make Mountain Array/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,9 @@ function minimumMountainRemovals(nums: number[]): number {
209209
}
210210
```
211211

212-
### **TypeScript**
212+
### **Rust**
213213

214-
```ts
214+
```rust
215215
impl Solution {
216216
pub fn minimum_mountain_removals(nums: Vec<i32>) -> i32 {
217217
let n = nums.len();

solution/1800-1899/1870.Minimum Speed to Arrive on Time/README.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,16 @@ int search(int left, int right) {
112112
}
113113
```
114114

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

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

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

126126
<!-- tabs:start -->
127127

solution/1800-1899/1870.Minimum Speed to Arrive on Time/README_EN.md

+11
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,17 @@ int search(int left, int right) {
102102
}
103103
```
104104

105+
When doing binary search problems, you can follow the following routine:
106+
107+
1. Write out the loop condition $left < right$;
108+
2. Inside the loop, you might as well write $mid = \lfloor \frac{left + right}{2} \rfloor$ first;
109+
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$);
110+
- 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$;
111+
- 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$;
112+
4. When the loop ends, $left$ equals $right$.
113+
114+
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.
115+
105116
<!-- tabs:start -->
106117

107118
### **Python3**

0 commit comments

Comments
 (0)