package leetcode;

/**
 * https://leetcode.com/problems/maximum-frequency-stack/
 */
public class Problem895 {
    private static class FreqStack {
        public FreqStack() {
        }

        public void push(int x) {
            // TODO
        }

        public int pop() {
            // TODO
            return 0;
        }
    }

    public static void main(String[] args) {
        FreqStack stack = new FreqStack();
        stack.push(5);
        stack.push(7);
        stack.push(5);
        stack.push(7);
        stack.push(4);
        stack.push(5);
        System.out.println(stack.pop()); // 5
        System.out.println(stack.pop()); // 7
        System.out.println(stack.pop()); // 5
        System.out.println(stack.pop()); // 4
    }
}

@@ -12,16 +12,89 @@
 tcode;%0A%0A
+import java.util.HashMap;%0Aimport java.util.Map;%0Aimport java.util.Stack;%0A%0A
 /**%0A * h
@@ -227,171 +227,535 @@
    p
-ublic FreqStack() %7B%0A        %7D%0A%0A        public void push(int x) %7B%0A            // TODO%0A        %7D%0A%0A        public int pop() %7B%0A            // TODO%0A            return 0
+rivate final Map%3CInteger /*number*/, Integer /*count*/%3E numCounts = new HashMap%3C%3E();%0A        private final Map%3CInteger /*count*/, Stack%3CInteger%3E%3E countStack = new HashMap%3C%3E();%0A        private int maxCount = 0;%0A%0A        public FreqStack() %7B%0A        %7D%0A%0A        public void push(int x) %7B%0A            int count;%0A            if (!numCounts.containsKey(x)) %7B%0A                count = 1;%0A                numCounts.put(x, count);%0A            %7D else %7B%0A                count = numCounts.get(x) + 1;%0A                numCounts.put(x, count)
 ;%0A  
@@ -760,18 +760,16 @@
         
-%7D%0A
     %7D%0A%0A 
@@ -775,57 +775,66 @@
     
-public static void main(String%5B%5D args) %7B%0A
+        if (!countStack.containsKey(count)) %7B%0A    
         
 Freq
@@ -829,25 +829,34 @@
         
-Freq
+    
 Stack
+%3CInteger%3E
  stack =
@@ -860,25 +860,23 @@
 k = new 
-Freq
 Stack
+%3C%3E
 ();%0A    
@@ -875,36 +875,43 @@
 ();%0A        
+  
-stack.push(5
+      stack.add(x
 );%0A        s
@@ -913,298 +913,635 @@
     
+  
-stack.push(7);%0A        stack.push(5);%0A        stack.push(7);%0A        stack.push(4);%0A        stack.push(5);%0A        System.out.println(stack.pop()); // 5%0A        System.out.println(stack.pop()); // 7%0A        System.out.println(stack.pop()); // 5%0A        System.out.println(stack.pop()); // 4
+      countStack.put(count, stack);%0A            %7D else %7B%0A                countStack.get(count).add(x);%0A            %7D%0A            maxCount = Math.max(maxCount, count);%0A        %7D%0A%0A        public int pop() %7B%0A            Stack%3CInteger%3E stack = countStack.get(maxCount);%0A            int val = stack.pop();%0A            if (stack.isEmpty()) %7B%0A                countStack.remove(maxCount--);%0A            %7D%0A            int count = numCounts.get(val) - 1;%0A            if (count == 0) %7B%0A                numCounts.remove(val);%0A            %7D else %7B%0A                numCounts.put(val, count);%0A            %7D%0A            return val;%0A        %7D
 %0A   

Solve problem 895