LeetCode 솔루션 분류

682. Baseball Game

컨텐츠 정보

본문

[Leetcode 시즌 3] 2022년 4월 10일 문제

[Easy] 682. Baseball Game


You are keeping score for a baseball game with strange rules. The game consists of several rounds, where the scores of past rounds may affect future rounds' scores.

At the beginning of the game, you start with an empty record. You are given a list of strings ops, where ops[i] is the ith operation you must apply to the record and is one of the following:

  1. An integer x - Record a new score of x.
  2. "+" - Record a new score that is the sum of the previous two scores. It is guaranteed there will always be two previous scores.
  3. "D" - Record a new score that is double the previous score. It is guaranteed there will always be a previous score.
  4. "C" - Invalidate the previous score, removing it from the record. It is guaranteed there will always be a previous score.

Return the sum of all the scores on the record.

 

Example 1:

Input: ops = ["5","2","C","D","+"]
Output: 30
Explanation:
"5" - Add 5 to the record, record is now [5].
"2" - Add 2 to the record, record is now [5, 2].
"C" - Invalidate and remove the previous score, record is now [5].
"D" - Add 2 * 5 = 10 to the record, record is now [5, 10].
"+" - Add 5 + 10 = 15 to the record, record is now [5, 10, 15].
The total sum is 5 + 10 + 15 = 30.

Example 2:

Input: ops = ["5","-2","4","C","D","9","+","+"]
Output: 27
Explanation:
"5" - Add 5 to the record, record is now [5].
"-2" - Add -2 to the record, record is now [5, -2].
"4" - Add 4 to the record, record is now [5, -2, 4].
"C" - Invalidate and remove the previous score, record is now [5, -2].
"D" - Add 2 * -2 = -4 to the record, record is now [5, -2, -4].
"9" - Add 9 to the record, record is now [5, -2, -4, 9].
"+" - Add -4 + 9 = 5 to the record, record is now [5, -2, -4, 9, 5].
"+" - Add 9 + 5 = 14 to the record, record is now [5, -2, -4, 9, 5, 14].
The total sum is 5 + -2 + -4 + 9 + 5 + 14 = 27.

Example 3:

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

 

Constraints:

  • 1 <= ops.length <= 1000
  • ops[i] is "C", "D", "+", or a string representing an integer in the range [-3 * 104, 3 * 104].
  • For operation "+", there will always be at least two previous scores on the record.
  • For operations "C" and "D", there will always be at least one previous score on the record.

관련자료

댓글 3

coderoncruise님의 댓글

  • 익명
  • 작성일
1. stack - SC:O(N)
2. pop() - TC: O(1)

class Solution:
    def calPoints(self, ops: List[str]) -> int:
        nums = []
        
        for op in ops:
            print(nums)
            if op == "+":
                nums.append(nums[-1] + nums[-2])
            elif op == "D":
                nums.append(nums[-1] * 2)
            elif op == "C":
                n1 = nums.pop()
            else:
                nums.append(int(op))
        
        
        return sum(nums)

mingki님의 댓글

  • 익명
  • 작성일
C++
Runtime: 3 ms, faster than 86.81% of C++ online submissions for Baseball Game.
Memory Usage: 8.3 MB, less than 69.90% of C++ online submissions for Baseball Game.
class Solution {
public:
    int calPoints(vector<string>& ops) {
        vector<int> scores;
        
        for (auto op : ops) {            
            if (op == "+") {
                scores.push_back(scores[scores.size() - 1] + scores[scores.size() - 2]);
            }
            else if (op == "D") {
                scores.push_back(scores[scores.size() - 1] * 2);
            }
            else if (op == "C") {
                scores.pop_back();
            }
            else {
                scores.push_back(stoi(op));
            }
        }
        return accumulate(scores.begin(), scores.end(), 0);
    }
};

bobkim님의 댓글

  • 익명
  • 작성일
class Solution {
public:
    int calPoints(vector<string>& ops) {
        std::vector<int> vec;
        int sum=0;
        for(int i=0;i<ops.size();i++){
            if(ops[i] == "C"){
                vec.pop_back();
            }else if(ops[i] == "D"){
                vec.push_back(vec.back()*2);
            }else if(ops[i] == "+"){
                vec.push_back(vec.back()+(*(vec.end()-2)));
            }else{
                vec.push_back(stoi(ops[i]));
            };
            
        };
        
        for(int i=0;i<vec.size();i++){
            sum+=vec[i];
        };
        
        return sum;   
    }
};
LeetCode 솔루션 2 / 1 페이지
번호
제목
이름

최근글


인기글


새댓글


Stats


  • 현재 접속자 748 명
  • 오늘 방문자 3,992 명
  • 어제 방문자 6,975 명
  • 최대 방문자 14,831 명
  • 전체 회원수 1,536 명
알림 0