Advertisement
pratiyush7

Untitled

Nov 3rd, 2021
731
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | None | 0 0
  1. static void superStack(String[] operations) {
  2.         List<Integer> stack = new ArrayList<>();
  3.         HashMap<Integer, Integer> map = new HashMap<>();
  4.         for (String s : operations) {
  5.             if (s.startsWith("push")) {
  6.                 int value = Integer.parseInt(s.split(" ")[1]);
  7.                 stack.add(value);
  8.             } else if (s.startsWith("pop")) {
  9.                 stack.remove(stack.size() - 1);
  10.                 //modify the hashmap
  11.                 for(Integer i: map.keySet()) {
  12.                     if (stack.size() < i) {
  13.                         map.put(i-1, map.get(i));
  14.                         map.remove(i);
  15.                     }
  16.                 }
  17.             } else if (s.startsWith("inc")) {
  18.                 String[] splits = s.split(" ");
  19.                 int e = Integer.parseInt(splits[1]);
  20.                 int value = Integer.parseInt(splits[2]);
  21.                 map.put(e, map.getOrDefault(e, 0) + value);
  22.             }
  23.  
  24.             if (stack.size() == 0) {
  25.                 System.out.println("EMPTY");
  26.             } else {
  27.                 int value = stack.get(stack.size()-1);
  28.                 for(Integer i: map.keySet()) {
  29.                     if (stack.size() <= i) {
  30.                         value+=map.get(i);
  31.                     }
  32.                 }
  33.                 System.out.println(value);
  34.             }
  35.         }
  36.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement