Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit Punch card activity. Click here for details.

Topic describes

This is a baseball game on LeetCode 682. The difficulty is easy.

Tag: “Simulation”

You are the recorder of a special baseball game. This match is made up of a number of rounds, and points scored in past rounds may affect points scored in future rounds.

At the start of the game, the record was blank. You get a string list of operations OPS, where OPS [I]ops[I]ops[I] is the third operation you need to log. Ops follows the following rules:

  1. The integerx– Indicates a new score for the roundx
  2. "+"– Indicates that the new score of the turn is the sum of the previous two scores. The question data ensures that this operation is always preceded by two valid scores.
  3. "D"– Indicates that the new score scored in this turn is twice the previous score. The question data ensures that this operation is always preceded by a valid score when recording it.
  4. "C"– Indicates that the previous score is invalid and removed from the record. The question data ensures that this operation is always preceded by a valid score when recording it.

Return the sum of all the scores in the record.

Example 1:

Ops = ["5","2","C","D","+"] "5" - record plus 5, record is now [5] "2" - record plus 2, record is now [5, 2] "C" - invalidate the record of the previous score and remove it, record is now [5]. "D" - Record plus 2 * 5 = 10, Record is now [5, 10]. "+" - Record plus 5 + 10 = 15, record is now [5, 10, 15]. The sum of all points 5 + 10 + 15 = 30Copy the code

Example 2:

Input: ops = "5", "2", "4", "C", "D", "9", "+", "+"] output: 27: "5" - record plus 5, record is now [5] "-2" - record plus -2, record is now [5, -2] "4" - record plus 4, record is now [5, -2, 4] "C" - invalidate the record of the previous score and remove it, Record is now a "D" - [5-2] record to add 2 * 2 = 4, record is now [5, 2, 4] "9" - record to add 9, record is now [5, 2, 4, 9] "+" - record - 4 + 9 = 5, The record is now [5, -2, -4, 9, 5] "+" - the record plus 9 + 5 = 14, the record is now [5, -2, -4, 9, 5, 14] the sum of all scores 5 + -2 + -4 + 9 + 5 + 14 = 27Copy the code

Example 3:

Input: ops = ["1"] Output: 1Copy the code

Tip:


  • 1 < = o p s . l e n g t h < = 1000 1 <= ops.length <= 1000

  • o p s [ i ] ops[i]
    "C","D","+", or a string representing an integer. The integer range is
    [ 3 1 0 4 . 3 1 0 4 ] [-3 * 10^4, 3 * 10^4]
  • for"+"Operation, the problem data ensures that recording this operation is always preceded by two valid scores
  • for"C""D"Operation, the question data ensures that this operation is always preceded by a valid score when recorded

simulation

Carry out the simulation according to the meaning of the question.

Code:

class Solution {
    static int[] nums = new int[1010];
    public int calPoints(String[] ops) {
        int n = ops.length, idx = 0;
        for (int i = 0; i < n; i++, idx++) {
            if (ops[i].equals("+")) nums[idx] = nums[idx - 1] + nums[idx - 2];    
            else if (ops[i].equals("D")) nums[idx] = nums[idx - 1] * 2;
            else if (ops[i].equals("C")) idx -= 2;
            else nums[idx] = Integer.parseInt(ops[i]);
        }
        int ans = 0;
        for (int i = 0; i < idx; i++) ans += nums[i];
        returnans; }}Copy the code


nums = [0 for _ in range(1010)]
class Solution:
    def calPoints(self, ops: List[str]) -> int:
        idx = 0
        for i in range(len(ops)):
            if ops[i] == '+':
                nums[idx] = nums[idx - 1] + nums[idx - 2]
            elif ops[i] == 'D':
                nums[idx] = nums[idx - 1] * 2
            elif ops[i] == 'C':
                idx -= 2
            else:
                nums[idx] = int(ops[i])
            idx += 1
        return sum(nums[i] for i in range(idx))
Copy the code
  • Time complexity: O(n)O(n)O(n)
  • Space complexity: O(n)O(n)O(n)

The last

This is the No.682 of our “Brush through LeetCode” series of articles. The series started on 2021/01/01.

In this series of articles, in addition to explaining how to solve the problem, I will also present the most concise code possible. If a general solution is involved, the corresponding code template will also be used.

In order to facilitate the students to debug and submit the code on the computer, I set up the relevant warehouse: github.com/SharingSour… .

In the repository, you’ll see links to the series of articles, the corresponding code for the series of articles, the LeetCode source links, and other preferred solutions.