-
[Leetcode] 682. Baseball Game 문제 풀이Algorithm/문제풀이 2022. 4. 10. 21:53
https://leetcode.com/problems/baseball-game/
Baseball Game - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
문제 내용
베이스볼 게임을 규칙대로 하는 데 규칙대로 하고 마지막에 최종 점수를 모두 합산한 금액 리턴
- An integer x - Record a new score of x.
- "+" - Record a new score that is the sum of the previous two scores. It is guaranteed there will always be two previous scores.
- "D" - Record a new score that is double the previous score. It is guaranteed there will always be a previous score.
- "C" - Invalidate the previous score, removing it from the record. It is guaranteed there will always be a previous score.
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.
문제 풀이
간단하게 규칙대로 하면 되는 문제이다
stack으로 풀려고 하다가 pop 하고 다시 push 하는 부분이 별로 마음에 들지 않아서 그냥 vector를 사용했다.
vector로 index참조하는게 빠를거라고 생각했다.
코드
class Solution { public: int calPoints(vector<string>& ops) { vector<int> points; for (auto& op : ops) { if (op.compare("+") == 0) { int a = points[points.size()-1]; int b = points[points.size()-2]; points.push_back(a+b); } else if (op.compare("D") == 0) { points.push_back(points.back() * 2); } else if (op.compare("C") == 0) { points.pop_back(); } else { points.push_back(stoi(op)); } } return reduce(points.begin(), points.end()); } };
'Algorithm > 문제풀이' 카테고리의 다른 글
[Leetcode] 289. Game of Life 문제 풀이 (0) 2022.04.12 [Leetcode] 1260. Shift 2D Grid 문제 풀이 (0) 2022.04.11 [Leetcode] 347. Top K Frequent Elements 문제 풀이 (0) 2022.04.09 [Leetcode] 703. Kth Largest Element in a Stream 문제 풀이 (0) 2022.04.08 [Leetcode] 1046. Last Stone Weight 문제 풀이 (0) 2022.04.07