Skip to content

Commit 9421b98

Browse files
committed
feat: update solutions to lc problems: No.0034,1870,1894
* No.0034.Find First and Last Position of Element in Sorted Array * No.1870.Minimum Speed to Arrive on Time * No.1894.Find the Student that Will Replace the Chalk
1 parent 8d168ed commit 9421b98

File tree

9 files changed

+54
-130
lines changed

9 files changed

+54
-130
lines changed

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

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@
4949

5050
**方法一:二分查找**
5151

52-
两遍二分,分别查找出左边界和右边界。
52+
我们可以进行两次二分查找,分别查找出左边界和右边界。
5353

54-
时间复杂度 $O(\log n)$。其中 $n$ 为数组长度
54+
时间复杂度 $O(\log n)$,空间复杂度 $O(1)$。其中 $n$ 是数组 `nums` 的长度
5555

5656
以下是二分查找的两个通用模板:
5757

@@ -189,23 +189,12 @@ var searchRange = function (nums, target) {
189189

190190
```go
191191
func searchRange(nums []int, target int) []int {
192-
search := func(x int) int {
193-
left, right := 0, len(nums)
194-
for left < right {
195-
mid := (left + right) >> 1
196-
if nums[mid] >= x {
197-
right = mid
198-
} else {
199-
left = mid + 1
200-
}
201-
}
202-
return left
203-
}
204-
l, r := search(target), search(target+1)
205-
if l == r {
206-
return []int{-1, -1}
207-
}
208-
return []int{l, r - 1}
192+
l := sort.Search(len(nums), func(i int) bool { return nums[i] >= target })
193+
r := sort.Search(len(nums), func(i int) bool { return nums[i] > target })
194+
if l == r {
195+
return []int{-1, -1}
196+
}
197+
return []int{l, r - 1}
209198
}
210199
```
211200

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

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -154,23 +154,12 @@ var searchRange = function (nums, target) {
154154

155155
```go
156156
func searchRange(nums []int, target int) []int {
157-
search := func(x int) int {
158-
left, right := 0, len(nums)
159-
for left < right {
160-
mid := (left + right) >> 1
161-
if nums[mid] >= x {
162-
right = mid
163-
} else {
164-
left = mid + 1
165-
}
166-
}
167-
return left
168-
}
169-
l, r := search(target), search(target+1)
170-
if l == r {
171-
return []int{-1, -1}
172-
}
173-
return []int{l, r - 1}
157+
l := sort.Search(len(nums), func(i int) bool { return nums[i] >= target })
158+
r := sort.Search(len(nums), func(i int) bool { return nums[i] > target })
159+
if l == r {
160+
return []int{-1, -1}
161+
}
162+
return []int{l, r - 1}
174163
}
175164
```
176165

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

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
func searchRange(nums []int, target int) []int {
2-
search := func(x int) int {
3-
left, right := 0, len(nums)
4-
for left < right {
5-
mid := (left + right) >> 1
6-
if nums[mid] >= x {
7-
right = mid
8-
} else {
9-
left = mid + 1
10-
}
11-
}
12-
return left
13-
}
14-
l, r := search(target), search(target+1)
2+
l := sort.Search(len(nums), func(i int) bool { return nums[i] >= target })
3+
r := sort.Search(len(nums), func(i int) bool { return nums[i] > target })
154
if l == r {
165
return []int{-1, -1}
176
}

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

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
7070

7171
二分枚举速度值,找到满足条件的最小速度。
7272

73+
时间复杂度 $O(n\times \log m)$,其中 $n$ 和 $m$ 分别为数组 `dist` 和最大速度值。
74+
7375
以下是二分查找的两个通用模板:
7476

7577
模板 1:
@@ -245,28 +247,22 @@ function arriveOnTime(dist, speed, hour) {
245247
```go
246248
func minSpeedOnTime(dist []int, hour float64) int {
247249
n := len(dist)
248-
left, right := 1, int(1e7)
249-
check := func(speed float64) bool {
250+
const mx int = 1e7 + 1
251+
x := sort.Search(mx, func(s int) bool {
252+
if s == 0 {
253+
return false
254+
}
250255
var cost float64
251256
for _, v := range dist[:n-1] {
252-
cost += math.Ceil(float64(v) / speed)
257+
cost += math.Ceil(float64(v) / float64(s))
253258
}
254-
cost += float64(dist[n-1]) / speed
259+
cost += float64(dist[n-1]) / float64(s)
255260
return cost <= hour
256-
257-
}
258-
for left < right {
259-
mid := (left + right) >> 1
260-
if check(float64(mid)) {
261-
right = mid
262-
} else {
263-
left = mid + 1
264-
}
265-
}
266-
if check(float64(left)) {
267-
return left
261+
})
262+
if x < 0 || x == mx {
263+
return -1
268264
}
269-
return -1
265+
return x
270266
}
271267
```
272268

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

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -222,28 +222,22 @@ function arriveOnTime(dist, speed, hour) {
222222
```go
223223
func minSpeedOnTime(dist []int, hour float64) int {
224224
n := len(dist)
225-
left, right := 1, int(1e7)
226-
check := func(speed float64) bool {
225+
const mx int = 1e7 + 1
226+
x := sort.Search(mx, func(s int) bool {
227+
if s == 0 {
228+
return false
229+
}
227230
var cost float64
228231
for _, v := range dist[:n-1] {
229-
cost += math.Ceil(float64(v) / speed)
232+
cost += math.Ceil(float64(v) / float64(s))
230233
}
231-
cost += float64(dist[n-1]) / speed
234+
cost += float64(dist[n-1]) / float64(s)
232235
return cost <= hour
233-
234-
}
235-
for left < right {
236-
mid := (left + right) >> 1
237-
if check(float64(mid)) {
238-
right = mid
239-
} else {
240-
left = mid + 1
241-
}
242-
}
243-
if check(float64(left)) {
244-
return left
236+
})
237+
if x < 0 || x == mx {
238+
return -1
245239
}
246-
return -1
240+
return x
247241
}
248242
```
249243

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
11
func minSpeedOnTime(dist []int, hour float64) int {
22
n := len(dist)
3-
left, right := 1, int(1e7)
4-
check := func(speed float64) bool {
3+
const mx int = 1e7 + 1
4+
x := sort.Search(mx, func(s int) bool {
5+
if s == 0 {
6+
return false
7+
}
58
var cost float64
69
for _, v := range dist[:n-1] {
7-
cost += math.Ceil(float64(v) / speed)
10+
cost += math.Ceil(float64(v) / float64(s))
811
}
9-
cost += float64(dist[n-1]) / speed
12+
cost += float64(dist[n-1]) / float64(s)
1013
return cost <= hour
11-
12-
}
13-
for left < right {
14-
mid := (left + right) >> 1
15-
if check(float64(mid)) {
16-
right = mid
17-
} else {
18-
left = mid + 1
19-
}
20-
}
21-
if check(float64(left)) {
22-
return left
14+
})
15+
if x < 0 || x == mx {
16+
return -1
2317
}
24-
return -1
18+
return x
2519
}

solution/1800-1899/1894.Find the Student that Will Replace the Chalk/README.md

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,7 @@ func chalkReplacer(chalk []int, k int) int {
176176
s[i+1] = s[i] + chalk[i]
177177
}
178178
k %= s[n]
179-
left, right := 0, n-1
180-
for left < right {
181-
mid := (left + right) >> 1
182-
if s[mid+1] > k {
183-
right = mid
184-
} else {
185-
left = mid + 1
186-
}
187-
}
188-
return left
179+
return sort.Search(n, func(i int) bool { return s[i+1] > k })
189180
}
190181
```
191182

solution/1800-1899/1894.Find the Student that Will Replace the Chalk/README_EN.md

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,7 @@ func chalkReplacer(chalk []int, k int) int {
119119
s[i+1] = s[i] + chalk[i]
120120
}
121121
k %= s[n]
122-
left, right := 0, n-1
123-
for left < right {
124-
mid := (left + right) >> 1
125-
if s[mid+1] > k {
126-
right = mid
127-
} else {
128-
left = mid + 1
129-
}
130-
}
131-
return left
122+
return sort.Search(n, func(i int) bool { return s[i+1] > k })
132123
}
133124
```
134125

solution/1800-1899/1894.Find the Student that Will Replace the Chalk/Solution.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,5 @@ func chalkReplacer(chalk []int, k int) int {
55
s[i+1] = s[i] + chalk[i]
66
}
77
k %= s[n]
8-
left, right := 0, n-1
9-
for left < right {
10-
mid := (left + right) >> 1
11-
if s[mid+1] > k {
12-
right = mid
13-
} else {
14-
left = mid + 1
15-
}
16-
}
17-
return left
8+
return sort.Search(n, func(i int) bool { return s[i+1] > k })
189
}

0 commit comments

Comments
 (0)