1
+ // 895. Maximum Frequency Stack
2
+ // Design a stack-like data structure to push elements to the stack and pop the most frequent element from the stack.
3
+
4
+ // Implement the FreqStack class:
5
+
6
+ // FreqStack() constructs an empty frequency stack.
7
+ // void push(int val) pushes an integer val onto the top of the stack.
8
+ // int pop() removes and returns the most frequent element in the stack.
9
+ // If there is a tie for the most frequent element, the element closest to the stack's top is removed and returned.
10
+
11
+ // Example 1:
12
+ // Input
13
+ // ["FreqStack", "push", "push", "push", "push", "push", "push", "pop", "pop", "pop", "pop"]
14
+ // [[], [5], [7], [5], [7], [4], [5], [], [], [], []]
15
+ // Output
16
+ // [null, null, null, null, null, null, null, 5, 7, 5, 4]
17
+
18
+ // Explanation
19
+ // FreqStack freqStack = new FreqStack();
20
+ // freqStack.push(5); // The stack is [5]
21
+ // freqStack.push(7); // The stack is [5,7]
22
+ // freqStack.push(5); // The stack is [5,7,5]
23
+ // freqStack.push(7); // The stack is [5,7,5,7]
24
+ // freqStack.push(4); // The stack is [5,7,5,7,4]
25
+ // freqStack.push(5); // The stack is [5,7,5,7,4,5]
26
+ // freqStack.pop(); // return 5, as 5 is the most frequent. The stack becomes [5,7,5,7,4].
27
+ // 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].
28
+ // freqStack.pop(); // return 5, as 5 is the most frequent. The stack becomes [5,7,4].
29
+ // 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].
30
+
31
+ // Constraints:
32
+ // 0 <= val <= 109
33
+ // At most 2 * 104 calls will be made to push and pop.
34
+ // It is guaranteed that there will be at least one element in the stack before calling pop.
35
+
36
+
37
+ // Microsoft
38
+ public class FreqStack {
39
+ Dictionary < int , int > DictFreq ;
40
+ Dictionary < int , Stack < int > > DictRecent ;
41
+ int maxFreq ;
42
+
43
+ public FreqStack ( ) {
44
+ DictFreq = new Dictionary < int , int > ( ) ;
45
+ DictRecent = new Dictionary < int , Stack < int > > ( ) ;
46
+ maxFreq = 0 ;
47
+ }
48
+
49
+ public void Push ( int val ) {
50
+ int freq ;
51
+ if ( ! DictFreq . ContainsKey ( val ) ) {
52
+ freq = 1 ;
53
+ }
54
+ else {
55
+ freq = DictFreq [ val ] + 1 ;
56
+ }
57
+ DictFreq [ val ] = freq ;
58
+ if ( ! DictRecent . ContainsKey ( freq ) ) {
59
+ Stack < int > st = new Stack < int > ( ) ;
60
+ st . Push ( val ) ;
61
+ DictRecent [ freq ] = st ;
62
+ }
63
+ else {
64
+ DictRecent [ freq ] . Push ( val ) ;
65
+ }
66
+ maxFreq = Math . Max ( maxFreq , freq ) ;
67
+ }
68
+
69
+ public int Pop ( ) {
70
+ int res ;
71
+ if ( DictFreq . Count == 0 )
72
+ return - 1 ;
73
+ res = DictRecent [ maxFreq ] . Pop ( ) ;
74
+ if ( DictRecent [ maxFreq ] . Count == 0 ) {
75
+ DictRecent . Remove ( maxFreq ) ;
76
+ maxFreq -- ;
77
+ }
78
+ DictFreq [ res ] -- ;
79
+ if ( DictFreq [ res ] == 0 ) {
80
+ DictFreq . Remove ( res ) ;
81
+ }
82
+ return res ;
83
+ }
84
+ }
85
+
86
+ /**
87
+ * Your FreqStack object will be instantiated and called as such:
88
+ * FreqStack obj = new FreqStack();
89
+ * obj.Push(val);
90
+ * int param_2 = obj.Pop();
91
+ */
0 commit comments