Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void superStack(String[] operations) {
- List<Integer> stack = new ArrayList<>();
- HashMap<Integer, Integer> map = new HashMap<>();
- for (String s : operations) {
- if (s.startsWith("push")) {
- int value = Integer.parseInt(s.split(" ")[1]);
- stack.add(value);
- } else if (s.startsWith("pop")) {
- stack.remove(stack.size() - 1);
- //modify the hashmap
- for(Integer i: map.keySet()) {
- if (stack.size() < i) {
- map.put(i-1, map.get(i));
- map.remove(i);
- }
- }
- } else if (s.startsWith("inc")) {
- String[] splits = s.split(" ");
- int e = Integer.parseInt(splits[1]);
- int value = Integer.parseInt(splits[2]);
- map.put(e, map.getOrDefault(e, 0) + value);
- }
- if (stack.size() == 0) {
- System.out.println("EMPTY");
- } else {
- int value = stack.get(stack.size()-1);
- for(Integer i: map.keySet()) {
- if (stack.size() <= i) {
- value+=map.get(i);
- }
- }
- System.out.println(value);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement