diff --git a/leetcode/1333.go b/leetcode/1333.go new file mode 100644 index 0000000..5f1fb0d --- /dev/null +++ b/leetcode/1333.go @@ -0,0 +1,36 @@ +/** + * @author: baowj + * @time: 2023/9/27 10:44 + */ +package main + +import "sort" + +func filterRestaurants(restaurants [][]int, veganFriendly int, maxPrice int, maxDistance int) []int { + res := [][]int{} + for _, r := range restaurants { + if veganFriendly == 1 && r[2] == 0 { + continue + } + if r[3] > maxPrice { + continue + } + if r[4] > maxDistance { + continue + } + res = append(res, r) + } + sort.Slice(res, func(i, j int) bool { + if res[i][1] == res[j][1] { + return res[i][0] > res[j][0] + } else { + return res[i][1] > res[j][1] + } + }) + + ans := make([]int, 0, len(res)) + for i := range res { + ans = append(ans, res[i][0]) + } + return ans +} diff --git a/leetcode/2074.go b/leetcode/2074.go new file mode 100644 index 0000000..3b7c2c1 --- /dev/null +++ b/leetcode/2074.go @@ -0,0 +1,69 @@ +/** + * @author: baowj + * @time: 2023/9/18 10:22 + */ +package main + +/** + * Definition for singly-linked list. + * type ListNode struct { + * Val int + * Next *ListNode + * } + */ +func reverseEvenLengthGroups(head *ListNode) *ListNode { + reverse := func(node *ListNode) (h *ListNode, t *ListNode) { + t = node + h = nil + for node != nil { + next := node.Next + + node.Next = h + h = node + node = next + } + return h, t + } + + node := head + length := 1 + cnt := 0 + h, t := head, (*ListNode)(nil) + for node != nil { + if cnt == 0 { + h = node + } + cnt++ + if cnt == length { + next := node.Next + if cnt%2 == 0 { + node.Next = nil + sh, st := reverse(h) + t.Next = sh + t = st + } else { + if t != nil { + t.Next = h + } + t = node + } + node = next + + cnt = 0 + length++ + } else { + node = node.Next + } + } + if cnt == 0 { + return head + } + if cnt%2 == 0 { + sh, st := reverse(h) + t.Next = sh + t = st + } else { + t.Next = h + } + return head +} diff --git a/leetcode/2582.go b/leetcode/2582.go new file mode 100644 index 0000000..554f360 --- /dev/null +++ b/leetcode/2582.go @@ -0,0 +1,15 @@ +/** + * @author: baowj + * @time: 2023/9/26 17:06 + */ +package main + +func passThePillow(n int, time int) int { + direction := time/(n-1) + 1 + time %= n - 1 + if direction%2 == 1 { + return 1 + time + } else { + return n - time + } +} diff --git a/leetcode/2591.go b/leetcode/2591.go new file mode 100644 index 0000000..d092623 --- /dev/null +++ b/leetcode/2591.go @@ -0,0 +1,29 @@ +/** + * @author: baowj + * @time: 2023/9/22 11:04 + */ +package main + +func distMoney(money int, children int) int { + if money == 4 && children == 1 { + return -1 + } + money -= children + if money < 0 { + return -1 + } + num := money / 7 + if num > children { + return children - 1 + } + if num == children { + if money-num*7 == 0 { + return children + } + return children - 1 + } + if money-num*7 == 3 && children-num == 1 { + return num - 1 + } + return num +} diff --git a/leetcode/449.go b/leetcode/449.go index b99bb77..8c6ec9d 100644 --- a/leetcode/449.go +++ b/leetcode/449.go @@ -20,7 +20,7 @@ import ( type Codec struct { } -func Constructor() Codec { +func Constructor449() Codec { return Codec{} } diff --git a/leetcode/460.go b/leetcode/460.go new file mode 100644 index 0000000..9e6e351 --- /dev/null +++ b/leetcode/460.go @@ -0,0 +1,106 @@ +/** + * @author: baowj + * @time: 2023/9/26 17:10 + */ +package main + +import ( + "container/heap" +) + +type LFUCache struct { + cache []int + frequency []int + timestamp []int + queue []keyTime + clock int + capacity int + num int +} + +type keyTime struct { + key int + frequency int + timestamp int +} + +func Constructor(capacity int) LFUCache { + cache := make([]int, 1e5+100) + frequency := make([]int, 1e5+100) + timestamp := make([]int, 1e5+100) + queue := []keyTime{} + for i := range cache { + cache[i] = -1 + } + + lfu := LFUCache{ + cache: cache, + frequency: frequency, + timestamp: timestamp, + queue: queue, + clock: 0, + capacity: capacity, + num: 0, + } + return lfu +} + +func (this *LFUCache) Len() int { + return len(this.queue) +} + +func (this *LFUCache) Pop() any { + res := this.queue[len(this.queue)-1] + this.queue = this.queue[:len(this.queue)-1] + return res +} + +func (this *LFUCache) Push(x any) { + this.queue = append(this.queue, x.(keyTime)) +} + +func (this *LFUCache) Less(i, j int) bool { + if this.queue[i].frequency == this.queue[j].frequency { + return this.queue[i].timestamp < this.queue[j].timestamp + } else { + return this.queue[i].frequency < this.queue[j].frequency + } +} + +func (this *LFUCache) Swap(i, j int) { + this.queue[i], this.queue[j] = this.queue[j], this.queue[i] +} + +func (this *LFUCache) Get(key int) int { + this.clock++ + if v := this.cache[key]; v != -1 { + this.frequency[key]++ + this.timestamp[key] = this.clock + heap.Push(this, keyTime{key: key, frequency: this.frequency[key], timestamp: this.timestamp[key]}) + return v + } + return -1 +} + +func (this *LFUCache) Put(key int, value int) { + if this.cache[key] == -1 { + if this.num >= this.capacity { + for { + popKey := heap.Pop(this).(keyTime) + if popKey.timestamp == this.timestamp[popKey.key] { + this.cache[popKey.key] = -1 + this.frequency[popKey.key] = 0 + this.timestamp[popKey.key] = 0 + this.num-- + break + } + } + } + this.num++ + } + this.clock++ + this.frequency[key]++ + this.cache[key] = value + this.timestamp[key] = this.clock + heap.Push(this, keyTime{key: key, frequency: this.frequency[key], timestamp: this.timestamp[key]}) +} diff --git a/leetcode/LCP 06.go b/leetcode/LCP 06.go new file mode 100644 index 0000000..a585b98 --- /dev/null +++ b/leetcode/LCP 06.go @@ -0,0 +1,13 @@ +/** + * @author: baowj + * @time: 2023/9/20 9:56 + */ +package main + +func minCount(coins []int) int { + res := 0 + for _, coin := range coins { + res += (coin + 1) / 2 + } + return res +}