add new solutions

This commit is contained in:
kanna5 2026-01-27 18:38:43 +09:00
parent 67cad91898
commit eb6ffe8114
No known key found for this signature in database
24 changed files with 933 additions and 14 deletions

View file

@ -1,20 +1,39 @@
package q1226
func abs(i int) int {
if i < 0 {
return -i
}
return i
import "sync"
var (
pick = sync.Mutex{}
forks = [5]sync.Mutex{}
)
type DiningPhilosophers struct {
}
func minTimeToVisitAllPoints(points [][]int) int {
time := 0
for i := 0; i < len(points)-1; i++ {
cur, next := points[i], points[i+1]
dx, dy := abs(cur[0]-next[0]), abs(cur[1]-next[1])
time += max(dx, dy)
}
return time
func (p *DiningPhilosophers) wantsToEat(
philosopher int,
pickLeftFork func(),
pickRightFork func(),
eat func(),
putLeftFork func(),
putRightFork func(),
) {
l, r := &forks[philosopher], &forks[(philosopher+1)%5]
pick.Lock()
l.Lock()
r.Lock()
pick.Unlock()
pickLeftFork()
pickRightFork()
eat()
putLeftFork()
l.Unlock()
putRightFork()
r.Unlock()
}
var _ = minTimeToVisitAllPoints
var _t = DiningPhilosophers{}
var _ = _t.wantsToEat