Skip to content

Commit

Permalink
feat: update solutions to lc problems: No.0034,1870,1894
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
yanglbme committed Nov 6, 2022
1 parent 8d168ed commit 9421b98
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@

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

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

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

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

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

```go
func searchRange(nums []int, target int) []int {
search := func(x int) int {
left, right := 0, len(nums)
for left < right {
mid := (left + right) >> 1
if nums[mid] >= x {
right = mid
} else {
left = mid + 1
}
}
return left
}
l, r := search(target), search(target+1)
if l == r {
return []int{-1, -1}
}
return []int{l, r - 1}
l := sort.Search(len(nums), func(i int) bool { return nums[i] >= target })
r := sort.Search(len(nums), func(i int) bool { return nums[i] > target })
if l == r {
return []int{-1, -1}
}
return []int{l, r - 1}
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,23 +154,12 @@ var searchRange = function (nums, target) {

```go
func searchRange(nums []int, target int) []int {
search := func(x int) int {
left, right := 0, len(nums)
for left < right {
mid := (left + right) >> 1
if nums[mid] >= x {
right = mid
} else {
left = mid + 1
}
}
return left
}
l, r := search(target), search(target+1)
if l == r {
return []int{-1, -1}
}
return []int{l, r - 1}
l := sort.Search(len(nums), func(i int) bool { return nums[i] >= target })
r := sort.Search(len(nums), func(i int) bool { return nums[i] > target })
if l == r {
return []int{-1, -1}
}
return []int{l, r - 1}
}
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
func searchRange(nums []int, target int) []int {
search := func(x int) int {
left, right := 0, len(nums)
for left < right {
mid := (left + right) >> 1
if nums[mid] >= x {
right = mid
} else {
left = mid + 1
}
}
return left
}
l, r := search(target), search(target+1)
l := sort.Search(len(nums), func(i int) bool { return nums[i] >= target })
r := sort.Search(len(nums), func(i int) bool { return nums[i] > target })
if l == r {
return []int{-1, -1}
}
Expand Down
30 changes: 13 additions & 17 deletions solution/1800-1899/1870.Minimum Speed to Arrive on Time/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@

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

时间复杂度 $O(n\times \log m)$,其中 $n$ 和 $m$ 分别为数组 `dist` 和最大速度值。

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

模板 1:
Expand Down Expand Up @@ -245,28 +247,22 @@ function arriveOnTime(dist, speed, hour) {
```go
func minSpeedOnTime(dist []int, hour float64) int {
n := len(dist)
left, right := 1, int(1e7)
check := func(speed float64) bool {
const mx int = 1e7 + 1
x := sort.Search(mx, func(s int) bool {
if s == 0 {
return false
}
var cost float64
for _, v := range dist[:n-1] {
cost += math.Ceil(float64(v) / speed)
cost += math.Ceil(float64(v) / float64(s))
}
cost += float64(dist[n-1]) / speed
cost += float64(dist[n-1]) / float64(s)
return cost <= hour

}
for left < right {
mid := (left + right) >> 1
if check(float64(mid)) {
right = mid
} else {
left = mid + 1
}
}
if check(float64(left)) {
return left
})
if x < 0 || x == mx {
return -1
}
return -1
return x
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,28 +222,22 @@ function arriveOnTime(dist, speed, hour) {
```go
func minSpeedOnTime(dist []int, hour float64) int {
n := len(dist)
left, right := 1, int(1e7)
check := func(speed float64) bool {
const mx int = 1e7 + 1
x := sort.Search(mx, func(s int) bool {
if s == 0 {
return false
}
var cost float64
for _, v := range dist[:n-1] {
cost += math.Ceil(float64(v) / speed)
cost += math.Ceil(float64(v) / float64(s))
}
cost += float64(dist[n-1]) / speed
cost += float64(dist[n-1]) / float64(s)
return cost <= hour

}
for left < right {
mid := (left + right) >> 1
if check(float64(mid)) {
right = mid
} else {
left = mid + 1
}
}
if check(float64(left)) {
return left
})
if x < 0 || x == mx {
return -1
}
return -1
return x
}
```

Expand Down
28 changes: 11 additions & 17 deletions solution/1800-1899/1870.Minimum Speed to Arrive on Time/Solution.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
func minSpeedOnTime(dist []int, hour float64) int {
n := len(dist)
left, right := 1, int(1e7)
check := func(speed float64) bool {
const mx int = 1e7 + 1
x := sort.Search(mx, func(s int) bool {
if s == 0 {
return false
}
var cost float64
for _, v := range dist[:n-1] {
cost += math.Ceil(float64(v) / speed)
cost += math.Ceil(float64(v) / float64(s))
}
cost += float64(dist[n-1]) / speed
cost += float64(dist[n-1]) / float64(s)
return cost <= hour

}
for left < right {
mid := (left + right) >> 1
if check(float64(mid)) {
right = mid
} else {
left = mid + 1
}
}
if check(float64(left)) {
return left
})
if x < 0 || x == mx {
return -1
}
return -1
return x
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +176,7 @@ func chalkReplacer(chalk []int, k int) int {
s[i+1] = s[i] + chalk[i]
}
k %= s[n]
left, right := 0, n-1
for left < right {
mid := (left + right) >> 1
if s[mid+1] > k {
right = mid
} else {
left = mid + 1
}
}
return left
return sort.Search(n, func(i int) bool { return s[i+1] > k })
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,7 @@ func chalkReplacer(chalk []int, k int) int {
s[i+1] = s[i] + chalk[i]
}
k %= s[n]
left, right := 0, n-1
for left < right {
mid := (left + right) >> 1
if s[mid+1] > k {
right = mid
} else {
left = mid + 1
}
}
return left
return sort.Search(n, func(i int) bool { return s[i+1] > k })
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,5 @@ func chalkReplacer(chalk []int, k int) int {
s[i+1] = s[i] + chalk[i]
}
k %= s[n]
left, right := 0, n-1
for left < right {
mid := (left + right) >> 1
if s[mid+1] > k {
right = mid
} else {
left = mid + 1
}
}
return left
return sort.Search(n, func(i int) bool { return s[i+1] > k })
}

0 comments on commit 9421b98

Please sign in to comment.