Skip to content

Commit

Permalink
2023/9/27
Browse files Browse the repository at this point in the history
  • Loading branch information
baowj-678 committed Sep 27, 2023
1 parent 10d1a5a commit 036598a
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 1 deletion.
36 changes: 36 additions & 0 deletions leetcode/1333.go
Original file line number Diff line number Diff line change
@@ -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
}
69 changes: 69 additions & 0 deletions leetcode/2074.go
Original file line number Diff line number Diff line change
@@ -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
}
15 changes: 15 additions & 0 deletions leetcode/2582.go
Original file line number Diff line number Diff line change
@@ -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
}
}
29 changes: 29 additions & 0 deletions leetcode/2591.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion leetcode/449.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
type Codec struct {
}

func Constructor() Codec {
func Constructor449() Codec {
return Codec{}
}

Expand Down
106 changes: 106 additions & 0 deletions leetcode/460.go
Original file line number Diff line number Diff line change
@@ -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]})
}
13 changes: 13 additions & 0 deletions leetcode/LCP 06.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 036598a

Please sign in to comment.