Skip to content

Commit ce54dc7

Browse files
committed
682.Baseball Game
1 parent 47e6f66 commit ce54dc7

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
You are keeping the scores for a baseball game with strange rules. At the beginning of the game, you start with an empty record.
3+
4+
You are given a list of strings operations, where operations[i] is the ith operation you must apply to the record and is one of the following:
5+
6+
- An integer x.
7+
Record a new score of x.
8+
- '+'.
9+
Record a new score that is the sum of the previous two scores.
10+
- 'D'.
11+
Record a new score that is the double of the previous score.
12+
- 'C'.
13+
Invalidate the previous score, removing it from the record.
14+
15+
Return the sum of all the scores on the record after applying all the operations.
16+
17+
The test cases are generated such that the answer and all intermediate calculations fit in a 32-bit integer and that all operations are valid.
18+
19+
20+
Example 1:
21+
Input: ops = ["5","2","C","D","+"]
22+
Output: 30
23+
Explanation:
24+
"5" - Add 5 to the record, record is now [5].
25+
"2" - Add 2 to the record, record is now [5, 2].
26+
"C" - Invalidate and remove the previous score, record is now [5].
27+
"D" - Add 2 * 5 = 10 to the record, record is now [5, 10].
28+
"+" - Add 5 + 10 = 15 to the record, record is now [5, 10, 15].
29+
The total sum is 5 + 10 + 15 = 30.
30+
31+
Example 2:
32+
Input: ops = ["5","-2","4","C","D","9","+","+"]
33+
Output: 27
34+
Explanation:
35+
"5" - Add 5 to the record, record is now [5].
36+
"-2" - Add -2 to the record, record is now [5, -2].
37+
"4" - Add 4 to the record, record is now [5, -2, 4].
38+
"C" - Invalidate and remove the previous score, record is now [5, -2].
39+
"D" - Add 2 * -2 = -4 to the record, record is now [5, -2, -4].
40+
"9" - Add 9 to the record, record is now [5, -2, -4, 9].
41+
"+" - Add -4 + 9 = 5 to the record, record is now [5, -2, -4, 9, 5].
42+
"+" - Add 9 + 5 = 14 to the record, record is now [5, -2, -4, 9, 5, 14].
43+
The total sum is 5 + -2 + -4 + 9 + 5 + 14 = 27.
44+
45+
Example 3:
46+
Input: ops = ["1","C"]
47+
Output: 0
48+
Explanation:
49+
"1" - Add 1 to the record, record is now [1].
50+
"C" - Invalidate and remove the previous score, record is now [].
51+
Since the record is empty, the total sum is 0.
52+
53+
54+
Constraints:
55+
- 1 <= operations.length <= 1000
56+
- operations[i] is "C", "D", "+", or a string representing an integer in the range [-3 * 104, 3 * 104].
57+
- For operation "+", there will always be at least two previous scores on the record.
58+
- For operations "C" and "D", there will always be at least one previous score on the record.
59+
*/
60+
class Solution {
61+
func calPoints(_ operations: [String]) -> Int {
62+
var numsArr = [Int]()
63+
for s in operations {
64+
if s == "C" {
65+
numsArr.popLast()
66+
} else if s == "D" {
67+
var newElement: Int = numsArr.last!
68+
newElement *= 2
69+
numsArr.append(newElement)
70+
} else if s == "+" {
71+
let last = numsArr.last!
72+
let previousLast = numsArr[numsArr.count - 2]
73+
let newElement = last + previousLast
74+
numsArr.append(newElement)
75+
} else {
76+
numsArr.append(Int(s)!)
77+
}
78+
}
79+
return numsArr.reduce(0, +)
80+
}
81+
}
82+
83+
let s = Solution()
84+
let r = s.calPoints(["5","2","C","D","+"])
85+
print(r)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Easy/682.Baseball Game.playground/playground.xcworkspace/contents.xcworkspacedata

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
118. [Robot Return to Origin](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/657.Robot%20Return%20to%20Origin.playground/Contents.swift)
123123
119. [Longest Continuous Increasing Subsequence](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/674.Longest%20Continuous%20Increasing%20Subsequence.playground/Contents.swift)
124124
120. [Valid Palindrome II](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/680.Valid%20Palindrome%20II.playground/Contents.swift)
125+
121. [Baseball Game](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/682.Baseball%20Game.playground/Contents.swift)
125126

126127
#### Medium
127128

0 commit comments

Comments
 (0)