package leetcode;

/**
 * https://leetcode.com/problems/design-a-stack-with-increment-operation/
 */
public class Problem1381 {
    static class CustomStack {
        public CustomStack(int maxSize) {
        }

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

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

        public void increment(int k, int val) {
            // TODO
        }
    }

    public static void main(String[] args) {
        CustomStack stack = new CustomStack(3);
        stack.push(1);
        stack.push(2);
        System.out.println(stack.pop()); // 2
        stack.push(2);
        stack.push(3);
        stack.push(4);
        stack.increment(5, 100);
        stack.increment(2, 100);
        System.out.println(stack.pop()); // 103
        System.out.println(stack.pop()); // 202
        System.out.println(stack.pop()); // 201
        System.out.println(stack.pop()); // -1
    }
}

@@ -12,16 +12,73 @@
 tcode;%0A%0A
+import java.util.Iterator;%0Aimport java.util.LinkedList;%0A%0A
 /**%0A * h
@@ -209,16 +209,302 @@
 Stack %7B%0A
+        private final int maxSize;%0A        private final LinkedList%3CIntWrapper%3E list = new LinkedList%3C%3E();%0A%0A        private static class IntWrapper %7B%0A            private int value;%0A%0A            public IntWrapper(int value) %7B%0A                this.value = value;%0A            %7D%0A        %7D%0A%0A
         
@@ -533,24 +533,60 @@
  maxSize) %7B%0A
+            this.maxSize = maxSize;%0A
         %7D%0A%0A 
@@ -634,35 +634,51 @@
     
-// TODO%0A   
+if (list.size() == maxSize) %7B%0A
      
-%7D%0A%0A
         
 publ
@@ -677,26 +677,18 @@
     
-public int pop() %7B
+   return;
 %0A   
@@ -688,39 +688,33 @@
 rn;%0A            
-// TODO
+%7D
 %0A            ret
@@ -714,105 +714,150 @@
     
-return 0;%0A        %7D%0A%0A        public void increment(int k, int val) %7B%0A            // TODO%0A
+list.push(new IntWrapper(x));%0A        %7D%0A%0A        public int pop() %7B%0A            if (list.size() == 0) %7B%0A                return -1;%0A   
         
 %7D%0A  
@@ -856,527 +856,373 @@
     
+ 
 %7D%0A    
-%7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        CustomStack stack = new CustomStack(3);%0A        stack.push(1);%0A        stack.push(2);%0A        System.out.println(stack.pop()); // 2%0A        stack.push(2);%0A        stack.push(3);%0A        stack.push(4);%0A        stack.increment(5, 100);%0A        stack.increment(2, 100);%0A        System.out.println(stack.pop()); // 103%0A        System.out.println(stack.pop()); // 202%0A        System.out.println(stack.pop()); // 201%0A        System.out.println(stack.pop()); // -1
+        return list.removeFirst().value;%0A        %7D%0A%0A        public void increment(int k, int val) %7B%0A            Iterator%3CIntWrapper%3E iter = list.descendingIterator();%0A            int i = 0;%0A            while (i %3C k && iter.hasNext()) %7B%0A                IntWrapper iw = iter.next();%0A                iw.value += val;%0A                i++;%0A            %7D%0A        %7D
 %0A   

Solve problem 1381