add new solutions
This commit is contained in:
parent
51975f3386
commit
489fa73880
13 changed files with 437 additions and 3 deletions
36
solutions/1/q110/solution.go
Normal file
36
solutions/1/q110/solution.go
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
package q110
|
||||
|
||||
type TreeNode struct {
|
||||
Val int
|
||||
Left *TreeNode
|
||||
Right *TreeNode
|
||||
}
|
||||
|
||||
func abs(a int) int {
|
||||
if a < 0 {
|
||||
return -a
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
func isBalancedInternal(node *TreeNode, lvl int) (bool, int) {
|
||||
if node == nil {
|
||||
return true, lvl
|
||||
}
|
||||
|
||||
lBal, lDpth := isBalancedInternal(node.Left, lvl+1)
|
||||
rBal, rDpth := isBalancedInternal(node.Right, lvl+1)
|
||||
dpth := max(lDpth, rDpth)
|
||||
|
||||
if !lBal || !rBal || abs(lDpth-rDpth) > 1 {
|
||||
return false, dpth
|
||||
}
|
||||
return true, dpth
|
||||
}
|
||||
|
||||
func isBalanced(root *TreeNode) bool {
|
||||
ret, _ := isBalancedInternal(root, 0)
|
||||
return ret
|
||||
}
|
||||
|
||||
var _ = isBalanced
|
||||
39
solutions/10/q1046/solution.go
Normal file
39
solutions/10/q1046/solution.go
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
package q1046
|
||||
|
||||
import "container/heap"
|
||||
|
||||
type IntHeap []int
|
||||
|
||||
func (h *IntHeap) Len() int { return len(*h) }
|
||||
func (h *IntHeap) Less(i int, j int) bool { return (*h)[i] > (*h)[j] }
|
||||
func (h *IntHeap) Push(x any) { *h = append(*h, x.(int)) }
|
||||
func (h *IntHeap) Swap(i int, j int) { (*h)[i], (*h)[j] = (*h)[j], (*h)[i] }
|
||||
|
||||
func (h *IntHeap) Pop() any {
|
||||
ret := (*h)[len(*h)-1]
|
||||
*h = (*h)[:len(*h)-1]
|
||||
return ret
|
||||
}
|
||||
|
||||
func lastStoneWeight(stones []int) int {
|
||||
var st IntHeap = stones
|
||||
heap.Init(&st)
|
||||
|
||||
for st.Len() > 1 {
|
||||
a, b := heap.Pop(&st).(int), heap.Pop(&st).(int)
|
||||
rem := a - b
|
||||
if rem < 0 {
|
||||
rem = -rem
|
||||
}
|
||||
if rem > 0 {
|
||||
heap.Push(&st, rem)
|
||||
}
|
||||
}
|
||||
|
||||
if st.Len() > 0 {
|
||||
return st[0]
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
var _ = lastStoneWeight
|
||||
37
solutions/13/q1382/solution.go
Normal file
37
solutions/13/q1382/solution.go
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
package q1382
|
||||
|
||||
type TreeNode struct {
|
||||
Val int
|
||||
Left *TreeNode
|
||||
Right *TreeNode
|
||||
}
|
||||
|
||||
func flatten(node *TreeNode, result *[]*TreeNode) {
|
||||
if node == nil {
|
||||
return
|
||||
}
|
||||
flatten(node.Left, result)
|
||||
*result = append(*result, node)
|
||||
flatten(node.Right, result)
|
||||
}
|
||||
|
||||
func reconstruct(nodes []*TreeNode) *TreeNode {
|
||||
if len(nodes) == 0 {
|
||||
return nil
|
||||
}
|
||||
mid := len(nodes) / 2
|
||||
root := nodes[mid]
|
||||
root.Left = reconstruct(nodes[:mid])
|
||||
root.Right = reconstruct(nodes[mid+1:])
|
||||
|
||||
return root
|
||||
}
|
||||
|
||||
func balanceBST(root *TreeNode) *TreeNode {
|
||||
nodes := make([]*TreeNode, 0, 128)
|
||||
flatten(root, &nodes)
|
||||
|
||||
return reconstruct(nodes)
|
||||
}
|
||||
|
||||
var _ = balanceBST
|
||||
18
solutions/16/q1653/solution.go
Normal file
18
solutions/16/q1653/solution.go
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
package q1653
|
||||
|
||||
func minimumDeletions(s string) int {
|
||||
bestA, bestB := 0, 0
|
||||
|
||||
for i := range len(s) {
|
||||
switch s[i] {
|
||||
case 'a':
|
||||
bestB++
|
||||
case 'b':
|
||||
bestA, bestB = bestA+1, min(bestA, bestB)
|
||||
}
|
||||
}
|
||||
|
||||
return min(bestA, bestB)
|
||||
}
|
||||
|
||||
var _ = minimumDeletions
|
||||
24
solutions/2/q232/solution.go
Normal file
24
solutions/2/q232/solution.go
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
package q232
|
||||
|
||||
type MyQueue struct{ data []int }
|
||||
|
||||
func Constructor() MyQueue { return MyQueue{} }
|
||||
func (q *MyQueue) Push(x int) { q.data = append(q.data, x) }
|
||||
|
||||
func (q *MyQueue) Pop() int {
|
||||
ret := q.data[0]
|
||||
q.data = q.data[1:]
|
||||
return ret
|
||||
}
|
||||
|
||||
func (q *MyQueue) Peek() int { return q.data[0] }
|
||||
func (q *MyQueue) Empty() bool { return len(q.data) == 0 }
|
||||
|
||||
/**
|
||||
* Your MyQueue object will be instantiated and called as such:
|
||||
* obj := Constructor();
|
||||
* obj.Push(x);
|
||||
* param_2 := obj.Pop();
|
||||
* param_3 := obj.Peek();
|
||||
* param_4 := obj.Empty();
|
||||
*/
|
||||
17
solutions/20/q2073/solution.go
Normal file
17
solutions/20/q2073/solution.go
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package q2073
|
||||
|
||||
func timeRequiredToBuy(tickets []int, k int) int {
|
||||
wants := tickets[k]
|
||||
|
||||
totalTime := 0
|
||||
for i := range tickets {
|
||||
if i <= k {
|
||||
totalTime += min(wants, tickets[i])
|
||||
} else {
|
||||
totalTime += min(wants-1, tickets[i])
|
||||
}
|
||||
}
|
||||
return totalTime
|
||||
}
|
||||
|
||||
var _ = timeRequiredToBuy
|
||||
115
solutions/30/q3013/solution.go
Normal file
115
solutions/30/q3013/solution.go
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
package q3013
|
||||
|
||||
import (
|
||||
"container/heap"
|
||||
"math"
|
||||
)
|
||||
|
||||
type HeapElem struct {
|
||||
pos int
|
||||
heapPos int
|
||||
}
|
||||
|
||||
type Heap struct {
|
||||
data []int
|
||||
h []*HeapElem
|
||||
isMin bool
|
||||
}
|
||||
|
||||
func (h *Heap) Len() int { return len(h.h) }
|
||||
func (h *Heap) TopVal() int { return h.data[h.h[0].pos] }
|
||||
|
||||
func (h *Heap) Less(i int, j int) bool {
|
||||
a, b := h.data[h.h[i].pos], h.data[h.h[j].pos]
|
||||
if h.isMin {
|
||||
return a < b
|
||||
}
|
||||
return a > b
|
||||
}
|
||||
|
||||
func (h *Heap) Push(x any) {
|
||||
elem := x.(*HeapElem)
|
||||
elem.heapPos = len(h.h)
|
||||
h.h = append(h.h, elem)
|
||||
}
|
||||
|
||||
func (h *Heap) Pop() any {
|
||||
ret := h.h[len(h.h)-1]
|
||||
h.h = h.h[:len(h.h)-1]
|
||||
ret.heapPos = -1
|
||||
return ret
|
||||
}
|
||||
|
||||
func (h *Heap) Swap(i int, j int) {
|
||||
h.h[i].heapPos = j
|
||||
h.h[j].heapPos = i
|
||||
h.h[i], h.h[j] = h.h[j], h.h[i]
|
||||
}
|
||||
|
||||
var (
|
||||
minHpElems = make([]HeapElem, 100_000)
|
||||
maxHpElems = make([]HeapElem, 100_000)
|
||||
minHeapBuf = make([]*HeapElem, 100_000)
|
||||
maxHeapBuf = make([]*HeapElem, 100_000)
|
||||
)
|
||||
|
||||
func minimumCost(nums []int, k int, dist int) int64 {
|
||||
minHpElems = minHpElems[:0]
|
||||
maxHpElems = maxHpElems[:0]
|
||||
for i := range nums {
|
||||
minHpElems = append(minHpElems, HeapElem{pos: i, heapPos: -1})
|
||||
maxHpElems = append(maxHpElems, HeapElem{pos: i, heapPos: -1})
|
||||
}
|
||||
maxHeap := Heap{data: nums, h: maxHeapBuf[:0], isMin: false}
|
||||
minHeap := Heap{data: nums, h: minHeapBuf[:0], isMin: true}
|
||||
|
||||
sum := nums[0]
|
||||
minSum := math.MaxInt
|
||||
|
||||
for i := 1; i < len(nums); i++ {
|
||||
remIdx := i - dist - 1
|
||||
if remIdx > 0 {
|
||||
if minHpElems[remIdx].heapPos != -1 {
|
||||
heap.Remove(&minHeap, minHpElems[remIdx].heapPos)
|
||||
} else if maxHpElems[remIdx].heapPos != -1 {
|
||||
heap.Remove(&maxHeap, maxHpElems[remIdx].heapPos)
|
||||
sum -= nums[remIdx]
|
||||
}
|
||||
}
|
||||
|
||||
if maxHeap.Len() < k-1 {
|
||||
if minHeap.Len() == 0 || nums[i] <= minHeap.TopVal() {
|
||||
heap.Push(&maxHeap, &maxHpElems[i])
|
||||
sum += nums[i]
|
||||
} else {
|
||||
heap.Push(&maxHeap, &maxHpElems[minHeap.h[0].pos])
|
||||
sum += nums[minHeap.h[0].pos]
|
||||
minHeap.h[0].heapPos = -1
|
||||
minHpElems[i].heapPos = 0
|
||||
minHeap.h[0] = &minHpElems[i]
|
||||
heap.Fix(&minHeap, 0)
|
||||
}
|
||||
|
||||
} else {
|
||||
heap.Push(&minHeap, &minHpElems[i])
|
||||
minTopV, maxTopV := minHeap.TopVal(), maxHeap.TopVal()
|
||||
if minTopV < maxTopV {
|
||||
sum += minTopV - maxTopV
|
||||
|
||||
minHeap.h[0].heapPos, maxHeap.h[0].heapPos = -1, -1
|
||||
minHeap.h[0], maxHeap.h[0] = &minHpElems[maxHeap.h[0].pos], &maxHpElems[minHeap.h[0].pos]
|
||||
minHeap.h[0].heapPos, maxHeap.h[0].heapPos = 0, 0
|
||||
heap.Fix(&minHeap, 0)
|
||||
heap.Fix(&maxHeap, 0)
|
||||
}
|
||||
}
|
||||
|
||||
if maxHeap.Len() == k-1 {
|
||||
minSum = min(minSum, sum)
|
||||
}
|
||||
}
|
||||
|
||||
return int64(minSum)
|
||||
}
|
||||
|
||||
var _ = minimumCost
|
||||
17
solutions/33/q3379/solution.go
Normal file
17
solutions/33/q3379/solution.go
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package q3379
|
||||
|
||||
func constructTransformedArray(nums []int) []int {
|
||||
n := len(nums)
|
||||
ret := make([]int, n)
|
||||
for i, num := range nums {
|
||||
num = (i + num) % n
|
||||
if num < 0 {
|
||||
num += n
|
||||
}
|
||||
ret[i] = nums[num]
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
var _ = constructTransformedArray
|
||||
33
solutions/36/q3634/solution.go
Normal file
33
solutions/36/q3634/solution.go
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
package q3634
|
||||
|
||||
import "slices"
|
||||
|
||||
const INF int = 1<<63 - 1
|
||||
|
||||
func minRemoval(nums []int, k int) int {
|
||||
slices.Sort(nums)
|
||||
minRem := INF
|
||||
|
||||
for i := 0; i < min(len(nums), minRem); i++ {
|
||||
if i > 0 && nums[i] == nums[i-1] {
|
||||
continue
|
||||
}
|
||||
target := nums[i] * k
|
||||
|
||||
l, r := i, len(nums)
|
||||
for l < r {
|
||||
m := (l + r) / 2
|
||||
switch {
|
||||
case nums[m] > target:
|
||||
r = m
|
||||
default:
|
||||
l = m + 1
|
||||
}
|
||||
}
|
||||
|
||||
minRem = min(minRem, i+len(nums)-l)
|
||||
}
|
||||
return minRem
|
||||
}
|
||||
|
||||
var _ = minRemoval
|
||||
33
solutions/36/q3637/solution.go
Normal file
33
solutions/36/q3637/solution.go
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
package q3637
|
||||
|
||||
func isTrionic(nums []int) bool {
|
||||
turns := 0
|
||||
isInc := true
|
||||
for i := 1; i < len(nums); i++ {
|
||||
if i == 1 && nums[i] < nums[i-1] {
|
||||
return false
|
||||
}
|
||||
|
||||
switch {
|
||||
case nums[i] == nums[i-1]:
|
||||
return false
|
||||
case nums[i] > nums[i-1]:
|
||||
if !isInc {
|
||||
isInc = true
|
||||
turns++
|
||||
}
|
||||
case nums[i] < nums[i-1]:
|
||||
if isInc {
|
||||
isInc = false
|
||||
turns++
|
||||
}
|
||||
}
|
||||
if turns > 2 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return turns == 2
|
||||
}
|
||||
|
||||
var _ = isTrionic
|
||||
59
solutions/36/q3640/solution.go
Normal file
59
solutions/36/q3640/solution.go
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
package q3640
|
||||
|
||||
import "math"
|
||||
|
||||
type node struct {
|
||||
pos int
|
||||
dir int8
|
||||
}
|
||||
|
||||
const (
|
||||
NEUTRAL int8 = iota
|
||||
ASCEND
|
||||
DESCEND
|
||||
)
|
||||
|
||||
func maxSumTrionic(nums []int) int64 {
|
||||
nodes := [4]node{}
|
||||
pfSum := make([]int, len(nums)+1) // prefix sum
|
||||
pfSum[1] = nums[0]
|
||||
var maxSum = math.MinInt
|
||||
|
||||
var currentDir int8
|
||||
for i := 1; i < len(nums); i++ {
|
||||
switch {
|
||||
case nums[i] == nums[i-1]:
|
||||
currentDir = NEUTRAL
|
||||
case nums[i] < nums[i-1]:
|
||||
currentDir = DESCEND
|
||||
default:
|
||||
currentDir = ASCEND
|
||||
}
|
||||
|
||||
if currentDir != nodes[3].dir {
|
||||
nodes[0], nodes[1], nodes[2] = nodes[1], nodes[2], nodes[3]
|
||||
nodes[3] = node{
|
||||
pos: i,
|
||||
dir: currentDir,
|
||||
}
|
||||
for j := nodes[0].pos + 1; j < nodes[1].pos; j++ {
|
||||
if pfSum[j] < pfSum[nodes[0].pos] {
|
||||
nodes[0].pos = j
|
||||
}
|
||||
}
|
||||
} else {
|
||||
nodes[3].pos = i
|
||||
}
|
||||
|
||||
pfSum[i+1] = nums[i] + pfSum[i]
|
||||
|
||||
if nodes[1].dir == ASCEND && nodes[2].dir == DESCEND && nodes[3].dir == ASCEND {
|
||||
curSum := pfSum[i+1] - pfSum[nodes[0].pos]
|
||||
maxSum = max(curSum, maxSum)
|
||||
}
|
||||
}
|
||||
|
||||
return int64(maxSum)
|
||||
}
|
||||
|
||||
var _ = maxSumTrionic
|
||||
Loading…
Add table
Add a link
Reference in a new issue