add new solutions
This commit is contained in:
parent
67cad91898
commit
eb6ffe8114
24 changed files with 933 additions and 14 deletions
29
solutions/12/q1200/solution.go
Normal file
29
solutions/12/q1200/solution.go
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
package q1200
|
||||
|
||||
import (
|
||||
"math"
|
||||
"slices"
|
||||
)
|
||||
|
||||
func minimumAbsDifference(arr []int) [][]int {
|
||||
slices.Sort(arr)
|
||||
|
||||
minDist := math.MaxInt
|
||||
for i := range len(arr) - 1 {
|
||||
minDist = min(minDist, arr[i+1]-arr[i])
|
||||
}
|
||||
|
||||
ret := [][]int{}
|
||||
for i := range len(arr) - 1 {
|
||||
if arr[i+1]-arr[i] == minDist {
|
||||
nums := []int{arr[i+1], arr[i]}
|
||||
if nums[0] > nums[1] {
|
||||
nums[0], nums[1] = nums[1], nums[0]
|
||||
}
|
||||
ret = append(ret, nums)
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
var _ = minimumAbsDifference
|
||||
|
|
@ -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
|
||||
|
|
|
|||
20
solutions/12/q1266/solution.go
Normal file
20
solutions/12/q1266/solution.go
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
package q1266
|
||||
|
||||
func abs(i int) int {
|
||||
if i < 0 {
|
||||
return -i
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
var _ = minTimeToVisitAllPoints
|
||||
Loading…
Add table
Add a link
Reference in a new issue