-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
38 lines (34 loc) · 1.1 KB
/
Copy pathsolution.java
File metadata and controls
38 lines (34 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// 895. Maximum Frequency Stack
// https://leetcode.com/problems/maximum-frequency-stack/
// Hard | Java | Accepted 2026-08-30
// Runtime 35 ms | Memory 70.5 MB
class FreqStack {
Map<Integer, Integer> freq = new HashMap<>();
Map<Integer, Stack<Integer>> groupFreq = new HashMap<>();
int mostFrequent = 0;
public FreqStack() {
}
// freq map (5, 1) (7, 1) (4, 0)
// Group freq map (1, [5, 7) (2, []) (3, [])
//mostFrequent 1
public void push(int val) {
freq.put(val, freq.getOrDefault(val, 0)+1);
groupFreq.computeIfAbsent(freq.get(val), k -> new Stack<>()).push(val);
mostFrequent = Math.max(mostFrequent, freq.get(val));
}
public int pop() {
int mostFreq = groupFreq.get(mostFrequent).pop();
if(groupFreq.get(mostFrequent).isEmpty())
{
mostFrequent--;
}
freq.put(mostFreq, freq.get(mostFreq)-1);
return mostFreq;
}
}
/**
* Your FreqStack object will be instantiated and called as such:
* FreqStack obj = new FreqStack();
* obj.push(val);
* int param_2 = obj.pop();
*/