type Queue struct{
queue []int}funcnewQueue()*Queue{return&Queue{
queue:make([]int,0),}}func(m *Queue)frontQue()int{return m.queue[0]}func(m *Queue)backQue()int{return m.queue[len(m.queue)-1]}func(m *Queue)isEmpty()bool{if(len(m.queue)==0){returntrue}returnfalse}func(m *Queue)popQue(val int){if(m.isEmpty()==false&& val == m.frontQue()){
m.queue = m.queue[1:]}}func(m *Queue)pushQue(val int){for m.isEmpty()==false&& val > m.backQue(){
m.queue = m.queue[:len(m.queue)-1]}
m.queue =append(m.queue,val)}funcmaxSlidingWindow(nums []int, k int)[]int{
queue :=newQueue()
res :=[]int{}for i :=0; i < k ; i++{
queue.pushQue(nums[i])}
res =append(res,queue.frontQue())for i := k ; i <len(nums); i++{
queue.popQue(nums[i - k])
queue.pushQue(nums[i])
res =append(res,queue.frontQue())}return res
}
347.前 K 个高频元素
functopKFrequent(nums []int, k int)[]int{
count :=make(map[int]int,0)for_,v :=range nums{
count[v]++}
ans :=[]int{}for k,_:=range count{
ans =append(ans , k)}
sort.Slice(ans ,func(a int, b int)bool{return count[ans[a]]> count[ans[b]]})return ans[:k]}