|
| 1 | +/** |
| 2 | + * Implement FreqStack, a class which simulates the operation of a stack-like |
| 3 | + * data structure. |
| 4 | + * |
| 5 | + * FreqStack has two functions: |
| 6 | + * push(int x), which pushes an integer x onto the stack. |
| 7 | + * pop(), which removes and returns the most frequent element in the stack. |
| 8 | + * |
| 9 | + * If there is a tie for most frequent element, the element closest to the top |
| 10 | + * of the stack is removed and returned. |
| 11 | + * |
| 12 | + * Example 1: |
| 13 | + * Input: |
| 14 | + * ["FreqStack","push","push","push","push","push","push","pop","pop","pop","pop"], |
| 15 | + * [[],[5],[7],[5],[7],[4],[5],[],[],[],[]] |
| 16 | + * Output: [null,null,null,null,null,null,null,5,7,5,4] |
| 17 | + * Explanation: |
| 18 | + * After making six .push operations, the stack is [5,7,5,7,4,5] from bottom |
| 19 | + * to top. Then: |
| 20 | + * |
| 21 | + * pop() -> returns 5, as 5 is the most frequent. |
| 22 | + * The stack becomes [5,7,5,7,4]. |
| 23 | + * |
| 24 | + * pop() -> returns 7, as 5 and 7 is the most frequent, but 7 is closest to the top. |
| 25 | + * The stack becomes [5,7,5,4]. |
| 26 | + * |
| 27 | + * pop() -> returns 5. |
| 28 | + * The stack becomes [5,7,4]. |
| 29 | + * |
| 30 | + * pop() -> returns 4. |
| 31 | + * The stack becomes [5,7]. |
| 32 | + * |
| 33 | + * Note: |
| 34 | + * Calls to FreqStack.push(int x) will be such that 0 <= x <= 10^9. |
| 35 | + * It is guaranteed that FreqStack.pop() won't be called if the stack has zero elements. |
| 36 | + * The total number of FreqStack.push calls will not exceed 10000 in a single test case. |
| 37 | + * The total number of FreqStack.pop calls will not exceed 10000 in a single test case. |
| 38 | + * The total number of FreqStack.push and FreqStack.pop calls will not exceed 150000 across all test cases. |
| 39 | + */ |
| 40 | + |
| 41 | +publicclassMaximumFrequencyStack895 { |
| 42 | +/** |
| 43 | + * https://leetcode.com/problems/maximum-frequency-stack/solution/ |
| 44 | + */ |
| 45 | +classFreqStack { |
| 46 | +Map<Integer,Integer>freq; |
| 47 | +Map<Integer,Stack<Integer>>group; |
| 48 | +intmaxfreq; |
| 49 | + |
| 50 | +publicFreqStack() { |
| 51 | +freq =newHashMap(); |
| 52 | +group =newHashMap(); |
| 53 | +maxfreq =0; |
| 54 | + } |
| 55 | + |
| 56 | +publicvoidpush(intx) { |
| 57 | +intf =freq.getOrDefault(x,0) +1; |
| 58 | +freq.put(x,f); |
| 59 | +if (f >maxfreq) |
| 60 | +maxfreq =f; |
| 61 | + |
| 62 | +group.computeIfAbsent(f,z->newStack()).push(x); |
| 63 | + } |
| 64 | + |
| 65 | +publicintpop() { |
| 66 | +intx =group.get(maxfreq).pop(); |
| 67 | +freq.put(x,freq.get(x) -1); |
| 68 | +if (group.get(maxfreq).size() ==0) |
| 69 | +maxfreq--; |
| 70 | +returnx; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | +/** |
| 76 | + * Your FreqStack object will be instantiated and called as such: |
| 77 | + * FreqStack obj = new FreqStack(); |
| 78 | + * obj.push(x); |
| 79 | + * int param_2 = obj.pop(); |
| 80 | + */ |
| 81 | + |
| 82 | +} |