add new solutions
This commit is contained in:
parent
886b5e0a8e
commit
67cad91898
47 changed files with 1549 additions and 1 deletions
23
solutions/8/q841/solution.go
Normal file
23
solutions/8/q841/solution.go
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
package q841
|
||||
|
||||
func canVisitAllRooms(rooms [][]int) bool {
|
||||
locked := len(rooms) - 1
|
||||
|
||||
opened := make([]bool, len(rooms))
|
||||
opened[0] = true
|
||||
|
||||
queue := []int{0}
|
||||
for ; len(queue) > 0; queue = queue[1:] {
|
||||
for _, i := range rooms[queue[0]] {
|
||||
if opened[i] {
|
||||
continue
|
||||
}
|
||||
opened[i] = true
|
||||
locked--
|
||||
queue = append(queue, i)
|
||||
}
|
||||
}
|
||||
return locked == 0
|
||||
}
|
||||
|
||||
var _ = canVisitAllRooms
|
||||
31
solutions/8/q875/solution.go
Normal file
31
solutions/8/q875/solution.go
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
package q875
|
||||
|
||||
import (
|
||||
"math"
|
||||
"slices"
|
||||
)
|
||||
|
||||
func canEat(piles []int, h, speed int) bool {
|
||||
needed := 0
|
||||
for _, n := range piles {
|
||||
needed += int(math.Ceil(float64(n) / float64(speed)))
|
||||
}
|
||||
return needed <= h
|
||||
}
|
||||
|
||||
func minEatingSpeed(piles []int, h int) int {
|
||||
maxK := slices.Max(piles)
|
||||
|
||||
l, r := 1, maxK+1
|
||||
for l < r {
|
||||
m := (l + r) / 2
|
||||
if canEat(piles, h, m) {
|
||||
r = m
|
||||
} else {
|
||||
l = m + 1
|
||||
}
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
var _ = minEatingSpeed
|
||||
Loading…
Add table
Add a link
Reference in a new issue