Skip to content

Commit 6830472

Browse files
committed
Maximum Frequency Stack
1 parent d734512 commit 6830472

File tree

2 files changed

+217
-0
lines changed

2 files changed

+217
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// 895. Maximum Frequency Stack
2+
// Topics: 'Array', 'Stack'
3+
// Level: 'Hard'
4+
5+
// Design a stack-like data structure to push elements to the stack and pop the most frequent element from the stack.
6+
7+
// Implement the FreqStack class:
8+
9+
// FreqStack() constructs an empty frequency stack.
10+
// void push(int val) pushes an integer val onto the top of the stack.
11+
// int pop() removes and returns the most frequent element in the stack.
12+
// If there is a tie for the most frequent element, the element closest to the stack's top is removed and returned.
13+
14+
// Example 1:
15+
16+
// Input
17+
// ["FreqStack", "push", "push", "push", "push", "push", "push", "pop", "pop", "pop", "pop"]
18+
// [[], [5], [7], [5], [7], [4], [5], [], [], [], []]
19+
// Output
20+
// [null, null, null, null, null, null, null, 5, 7, 5, 4]
21+
22+
// Explanation
23+
// FreqStack freqStack = new FreqStack();
24+
// freqStack.push(5); // The stack is [5]
25+
// freqStack.push(7); // The stack is [5,7]
26+
// freqStack.push(5); // The stack is [5,7,5]
27+
// freqStack.push(7); // The stack is [5,7,5,7]
28+
// freqStack.push(4); // The stack is [5,7,5,7,4]
29+
// freqStack.push(5); // The stack is [5,7,5,7,4,5]
30+
// freqStack.pop(); // return 5, as 5 is the most frequent. The stack becomes [5,7,5,7,4].
31+
// freqStack.pop(); // return 7, as 5 and 7 is the most frequent, but 7 is closest to the top. The stack becomes [5,7,5,4].
32+
// freqStack.pop(); // return 5, as 5 is the most frequent. The stack becomes [5,7,4].
33+
// freqStack.pop(); // return 4, as 4, 5 and 7 is the most frequent, but 4 is closest to the top. The stack becomes [5,7].
34+
35+
// Constraints:
36+
37+
// 0 <= val <= 109
38+
// At most 2 * 104 calls will be made to push and pop.
39+
// It is guaranteed that there will be at least one element in the stack before calling pop.
40+
41+
package maximumfrequencystack
42+
43+
type FreqStack struct {
44+
max int
45+
stacks map[int][]int
46+
freq map[int]int
47+
}
48+
49+
func Constructor() FreqStack {
50+
return FreqStack{
51+
max: 1,
52+
freq: map[int]int{},
53+
stacks: map[int][]int{},
54+
}
55+
}
56+
57+
func (f *FreqStack) Push(val int) {
58+
fq, ok := f.freq[val]
59+
if !ok {
60+
fq = 1
61+
} else {
62+
fq++
63+
}
64+
f.freq[val] = fq
65+
stack, ok := f.stacks[fq]
66+
if !ok {
67+
stack = []int{val}
68+
} else {
69+
stack = append(stack, val)
70+
}
71+
f.stacks[fq] = stack
72+
if fq > f.max {
73+
f.max = fq
74+
}
75+
}
76+
77+
func (f *FreqStack) Pop() int {
78+
stack := f.stacks[f.max]
79+
v := stack[len(stack)-1]
80+
f.stacks[f.max] = stack[:len(stack)-1]
81+
if len(f.stacks[f.max]) == 0 {
82+
delete(f.stacks, f.max)
83+
f.max--
84+
}
85+
f.freq[v]--
86+
return v
87+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package maximumfrequencystack
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestFreqStack(t *testing.T) {
8+
freqStack := Constructor()
9+
freqStack.Push(5)
10+
freqStack.Push(7)
11+
freqStack.Push(5)
12+
freqStack.Push(7)
13+
freqStack.Push(4)
14+
freqStack.Push(5)
15+
16+
if got := freqStack.Pop(); got != 5 {
17+
t.Errorf("Pop() = %v, want %v", got, 5)
18+
}
19+
if got := freqStack.Pop(); got != 7 {
20+
t.Errorf("Pop() = %v, want %v", got, 7)
21+
}
22+
if got := freqStack.Pop(); got != 5 {
23+
t.Errorf("Pop() = %v, want %v", got, 5)
24+
}
25+
if got := freqStack.Pop(); got != 4 {
26+
t.Errorf("Pop() = %v, want %v", got, 4)
27+
}
28+
}
29+
30+
func TestFreqStackSingleElement(t *testing.T) {
31+
freqStack := Constructor()
32+
freqStack.Push(1)
33+
34+
if got := freqStack.Pop(); got != 1 {
35+
t.Errorf("Pop() = %v, want %v", got, 1)
36+
}
37+
}
38+
39+
func TestFreqStackAllSameElement(t *testing.T) {
40+
freqStack := Constructor()
41+
freqStack.Push(3)
42+
freqStack.Push(3)
43+
freqStack.Push(3)
44+
45+
if got := freqStack.Pop(); got != 3 {
46+
t.Errorf("Pop() = %v, want %v", got, 3)
47+
}
48+
if got := freqStack.Pop(); got != 3 {
49+
t.Errorf("Pop() = %v, want %v", got, 3)
50+
}
51+
if got := freqStack.Pop(); got != 3 {
52+
t.Errorf("Pop() = %v, want %v", got, 3)
53+
}
54+
}
55+
56+
func TestFreqStackMultipleOperations(t *testing.T) {
57+
freqStack := Constructor()
58+
freqStack.Push(1)
59+
freqStack.Push(2)
60+
freqStack.Push(1)
61+
62+
if got := freqStack.Pop(); got != 1 {
63+
t.Errorf("Pop() = %v, want %v", got, 1)
64+
}
65+
66+
freqStack.Push(3)
67+
freqStack.Push(2)
68+
69+
if got := freqStack.Pop(); got != 2 {
70+
t.Errorf("Pop() = %v, want %v", got, 2)
71+
}
72+
if got := freqStack.Pop(); got != 3 {
73+
t.Errorf("Pop() = %v, want %v", got, 3)
74+
}
75+
if got := freqStack.Pop(); got != 2 {
76+
t.Errorf("Pop() = %v, want %v", got, 2)
77+
}
78+
if got := freqStack.Pop(); got != 1 {
79+
t.Errorf("Pop() = %v, want %v", got, 1)
80+
}
81+
}
82+
83+
func TestFreqStackTieBreaker(t *testing.T) {
84+
freqStack := Constructor()
85+
freqStack.Push(1)
86+
freqStack.Push(2)
87+
freqStack.Push(3)
88+
89+
if got := freqStack.Pop(); got != 3 {
90+
t.Errorf("Pop() = %v, want %v", got, 3)
91+
}
92+
if got := freqStack.Pop(); got != 2 {
93+
t.Errorf("Pop() = %v, want %v", got, 2)
94+
}
95+
if got := freqStack.Pop(); got != 1 {
96+
t.Errorf("Pop() = %v, want %v", got, 1)
97+
}
98+
}
99+
100+
func TestFreqStackComplexScenario(t *testing.T) {
101+
freqStack := Constructor()
102+
freqStack.Push(4)
103+
freqStack.Push(0)
104+
freqStack.Push(9)
105+
freqStack.Push(3)
106+
freqStack.Push(4)
107+
freqStack.Push(2)
108+
109+
if got := freqStack.Pop(); got != 4 {
110+
t.Errorf("Pop() = %v, want %v", got, 4)
111+
}
112+
113+
freqStack.Push(6)
114+
115+
if got := freqStack.Pop(); got != 6 {
116+
t.Errorf("Pop() = %v, want %v", got, 6)
117+
}
118+
119+
freqStack.Push(1)
120+
121+
if got := freqStack.Pop(); got != 1 {
122+
t.Errorf("Pop() = %v, want %v", got, 1)
123+
}
124+
125+
freqStack.Push(1)
126+
127+
if got := freqStack.Pop(); got != 1 {
128+
t.Errorf("Pop() = %v, want %v", got, 1)
129+
}
130+
}

0 commit comments

Comments
 (0)