add new solutions
This commit is contained in:
parent
886b5e0a8e
commit
67cad91898
47 changed files with 1549 additions and 1 deletions
32
solutions/23/q2300/solution.go
Normal file
32
solutions/23/q2300/solution.go
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
package q2300
|
||||
|
||||
import "slices"
|
||||
|
||||
func nGte(arr []int, target int) int {
|
||||
l, r := 0, len(arr)
|
||||
for l < r {
|
||||
m := (l + r) / 2
|
||||
if arr[m] >= target {
|
||||
r = m
|
||||
} else {
|
||||
l = m + 1
|
||||
}
|
||||
}
|
||||
return len(arr) - l
|
||||
}
|
||||
|
||||
func successfulPairs(spells []int, potions []int, success int64) []int {
|
||||
slices.Sort(potions)
|
||||
|
||||
for i := range spells {
|
||||
pow := spells[i]
|
||||
want := int(success) / pow
|
||||
if pow*want < int(success) {
|
||||
want++
|
||||
}
|
||||
spells[i] = nGte(potions, want)
|
||||
}
|
||||
return spells
|
||||
}
|
||||
|
||||
var _ = successfulPairs
|
||||
55
solutions/23/q2336/solution.go
Normal file
55
solutions/23/q2336/solution.go
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
package q2336
|
||||
|
||||
import "container/heap"
|
||||
|
||||
type MinHeap []int
|
||||
|
||||
func (m *MinHeap) Len() int { return len(*m) }
|
||||
func (m *MinHeap) Less(i int, j int) bool { return (*m)[i] < (*m)[j] }
|
||||
func (m *MinHeap) Push(x any) { *m = append(*m, x.(int)) }
|
||||
func (m *MinHeap) Swap(i int, j int) { (*m)[i], (*m)[j] = (*m)[j], (*m)[i] }
|
||||
|
||||
func (m *MinHeap) Pop() any {
|
||||
ret := (*m)[len(*m)-1]
|
||||
*m = (*m)[:len(*m)-1]
|
||||
return ret
|
||||
}
|
||||
|
||||
type SmallestInfiniteSet struct {
|
||||
head int
|
||||
added MinHeap
|
||||
}
|
||||
|
||||
func Constructor() SmallestInfiniteSet {
|
||||
return SmallestInfiniteSet{
|
||||
head: 1,
|
||||
added: MinHeap{},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SmallestInfiniteSet) PopSmallest() int {
|
||||
if s.added.Len() > 0 {
|
||||
top := s.added[0]
|
||||
for s.added.Len() > 0 && s.added[0] == top {
|
||||
heap.Pop(&s.added)
|
||||
}
|
||||
return top
|
||||
} else {
|
||||
s.head++
|
||||
return s.head - 1
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SmallestInfiniteSet) AddBack(num int) {
|
||||
if num >= s.head {
|
||||
return
|
||||
}
|
||||
heap.Push(&s.added, num)
|
||||
}
|
||||
|
||||
/**
|
||||
* Your SmallestInfiniteSet object will be instantiated and called as such:
|
||||
* obj := Constructor();
|
||||
* param_1 := obj.PopSmallest();
|
||||
* obj.AddBack(num);
|
||||
*/
|
||||
39
solutions/23/q2352/solution.go
Normal file
39
solutions/23/q2352/solution.go
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
package q2352
|
||||
|
||||
import "strings"
|
||||
|
||||
func index(grid [][]int, col bool, i int) string {
|
||||
b := strings.Builder{}
|
||||
b.Grow(len(grid) * 3)
|
||||
for j := range grid {
|
||||
var num int
|
||||
switch {
|
||||
case col: // ith col
|
||||
num = grid[j][i]
|
||||
default: // ith row
|
||||
num = grid[i][j]
|
||||
}
|
||||
|
||||
for range 3 {
|
||||
b.WriteByte(byte(num % 256))
|
||||
num >>= 8
|
||||
}
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func equalPairs(grid [][]int) int {
|
||||
idx := make(map[string]int, len(grid))
|
||||
nEq := 0
|
||||
for row := range grid {
|
||||
s := index(grid, false, row)
|
||||
idx[s]++
|
||||
}
|
||||
for col := range grid {
|
||||
s := index(grid, true, col)
|
||||
nEq += idx[s]
|
||||
}
|
||||
return nEq
|
||||
}
|
||||
|
||||
var _ = equalPairs
|
||||
Loading…
Add table
Add a link
Reference in a new issue