LeetCode.682 baseball game stack (simple)

You are now the recorder of a baseball game with a special game system. This game consists of several rounds. The scores in the past few rounds may affect the scores in the next few rounds.

At the beginning of the game, the record was blank. You will get a string list of recorded operations ops, where ops[i] is the ith operation you need to record. ops follows the following rules:

  1. Integer x - indicates the new score x obtained in this round
  2. "+" - indicates that the new score obtained in this round is the sum of the previous two scores. The title data ensures that there are always two valid scores in front of this operation.
  3. "D" - indicates that the score newly obtained in this round is twice that of the previous round. The title data ensures that there is always a valid score in front of this operation.
  4. "C" - indicates that the previous score is invalid and will be removed from the record. The title data ensures that there is always a valid score in front of this operation.
    Please return the sum of all scores in the record.

Example 1:

Input: ops = ["5", "2", "C", "D", "+"]
Output: 30
Explanation:
"5" - record plus 5, the record is now [5]
"2" - record plus 2, the record is now [5, 2]
"C" - invalidate and remove the record of the previous score. The record is now [5]
"D" - record plus 2 * 5 = 10, the record is now [5, 10]
"+" - record plus 5 + 10 = 15, the record is now [5, 10, 15]
Sum of all scores 5 + 10 + 15 = 30

Example 2:

Input: ops = ["5", "- 2", "4", "C", "D", "9", "+", "+"]
Output: 27
Explanation:
"5" - record plus 5, the record is now [5]
"- 2" - record plus - 2, record is now [5, - 2]
"4" - record plus 4, the record is now [5, - 2, 4]
"C" - invalidate and remove the record of the previous score. The record is now [5, - 2]
"D" - record plus 2 * - 2 = - 4, record is now [5, - 2, - 4]
"9" - record plus 9, the record is now [5, - 2, - 4, 9]
"+" - record plus - 4 + 9 = 5, record is now [5, - 2, - 4, 9, 5]
"+" - record plus 9 + 5 = 14, record is now [5, - 2, - 4, 9, 5, 14]
Sum of all scores 5 + - 2 + - 4 + 9 + 5 + 14 = 27

Example 3:

Input: ops = ["1"]
Output: 1

Tips:

  • 1 <= ops.length <= 1000
  • ops[i] is "C", "D", "plus", or a string representing an integer. The integer range is [- 3 * 104, 3 * 104]
  • For the "+" operation, the title data ensures that there are always two valid scores in front of this operation
  • For "C" and "D" operations, the title data ensures that there is always a valid score before recording this operation

This is a problem that can be solved by yourself without looking at the solution of the problem. Tears
if... else push pop

class Solution {
    public int calPoints(String[] ops) {
        Deque<Integer> stack = new ArrayDeque<>();
        for(String s : ops){
            if(s.equals("C")){
                stack.pop();
            }else if(s.equals("D")){
                stack.push(Integer.valueOf(stack.peek() * 2));
            }else if(s.equals("+")){
                int s1 = stack.peek();
                stack.pop();
                int s2 = stack.peek();
                stack.push(s1);
                stack.push(Integer.valueOf(s1 + s2));
            }else{
                stack.push(Integer.valueOf(s));
            }
        }
        int sum = 0;
        while(!stack.isEmpty()){
            int temp = stack.peek();
            sum += temp;
            stack.pop();
        }
        return sum;
    }
}

This is the answer submitted for the first time. After looking at the solution, optimize the solution steps:

class Solution {
    public int calPoints(String[] ops) {
        Deque<Integer> stack = new ArrayDeque<>();
        for(String s : ops){
            if(s.equals("C")){
                stack.pop();
            }else if(s.equals("D")){
                stack.push(stack.peek() * 2);
            }else if(s.equals("+")){
                int s1 = stack.pop();
                int s2 = stack.peek();
                stack.push(s1);
                stack.push(s1 + s2);
            }else{
                stack.push(Integer.valueOf(s));
            }
        }
        int sum = 0;
        while(!stack.isEmpty()){
            sum += stack.pop();
        }
        return sum;
    }
}

Complexity analysis

Time complexity: O(N), where N is the length of ops. We parse each element in a given array, and then each element performs the work of O(1).

Space complexity: O(N), the space used to store the stack.

Tags: Algorithm leetcode stack

Posted on Sat, 04 Dec 2021 22:25:02 -0500 by qartis