forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
9 changed files
with
54 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 2 additions & 13 deletions
15
solution/0000-0099/0034.Find First and Last Position of Element in Sorted Array/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 11 additions & 17 deletions
28
solution/1800-1899/1870.Minimum Speed to Arrive on Time/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters