Approach #1: Stack [Accepted]

Intuition and Algorithm

Let's maintain the value of each valid round on a stack as we process the data. A stack is ideal since we only deal with operations involving the last or second-last valid round.

Complexity Analysis

  • Time Complexity: , where is the length of ops. We parse through every element in the given array once, and do work for each element.

  • Space Complexity: , the space used to store our stack.

Analysis written by: @awice