Event counter with rollback (savepoint idea)
Java
Hard
4 views
Problem Description
Task: apply updates (+x) but also handle rollback to previous savepoint index. Return final value.
Output Format
Return value
Constraints
Use stack to store history.
Official Solution
static int applyWithRollback(int[] ops){java.util.ArrayDeque<Integer> st=new java.util.ArrayDeque<>();int val=0;for(int op:ops){if(op==0){if(!st.isEmpty()) val=st.pop();}else{st.push(val);val+=op;}}return val;}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!