From 7d2a3bc5e879eaeebf9ee861906335f29f7cb239 Mon Sep 17 00:00:00 2001 From: fengzw Date: Sat, 29 May 2021 22:40:59 +0800 Subject: [PATCH] update: original solution for pattern13 challenge3 --- .../Challenge3/solution.go | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/Pattern13 - Top K Elements/Challenge3/solution.go b/Pattern13 - Top K Elements/Challenge3/solution.go index f3cf50e..1819858 100644 --- a/Pattern13 - Top K Elements/Challenge3/solution.go +++ b/Pattern13 - Top K Elements/Challenge3/solution.go @@ -18,35 +18,34 @@ ref: https://leetcode-cn.com/problems/maximum-frequency-stack/ */ type FreqStack struct { - Stack []int - Heap FreqHeap - Map map[int]*Frequency + Heap FreqHeap + Map map[int]int + SeqNumber int } func Constructor() FreqStack { fs := FreqStack{} heap.Init(&fs.Heap) - fs.Map = make(map[int]*Frequency) + fs.Map = make(map[int]int) + fs.SeqNumber = 0 return fs } func (this *FreqStack) Push(val int) { - this.Stack = append(this.Stack, val) - if f, ok := this.Map[val]; ok { - f.LastInd = len(this.Stack) - 1 - f.Times++ - } else { - this.Map[val] = &Frequency{val, 1, len(this.Stack) - 1} - } - heap.Push(&this.Heap, *this.Map[val]) + this.Map[val]++ + this.SeqNumber++ + heap.Push(&this.Heap, Frequency{val, this.Map[val], this.SeqNumber}) } func (this *FreqStack) Pop() int { x := heap.Pop(&this.Heap).(Frequency).Val - this.Map[x].Times-- - if this.Map[x].Times == 0 { + + if this.Map[x] > 1 { + this.Map[x]-- + } else { delete(this.Map, x) } + return x }