Skip to content

Commit d6dd9cb

Browse files
committed
BaseballGame682
1 parent 6f8640a commit d6dd9cb

2 files changed

Lines changed: 81 additions & 2 deletions

File tree

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@
4444
| [Graph](https://github.com/fluency03/leetcode-java/blob/master/src/graph) |
4545

4646

47-
# Total: 357
47+
# Total: 358
4848

4949
| Easy | Medium | Hard | - |
5050
|:----:|:-------:|:----:|:-:|
51-
| 94 | 198 | 61 | 4 |
51+
| 95 | 198 | 61 | 4 |
5252

5353

5454
| Question | Solution | Difficulty |
@@ -375,6 +375,7 @@
375375
| [678. Valid Parenthesis String](https://leetcode.com/problems/valid-parenthesis-string/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/ValidParenthesisString678.java) | Medium |
376376
| [680. Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/ValidPalindromeII680.java) | Easy |
377377
| [681. Next Closest Time](https://leetcode.com/problems/next-closest-time/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/NextClosestTime681.java) | Medium |
378+
| [682. Baseball Game](https://leetcode.com/problems/baseball-game/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/BaseballGame682.java) | Easy |
378379
| [683. K Empty Slots](https://leetcode.com/problems/k-empty-slots/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/KEmptySlots683.java) | Hard |
379380
| [684. Redundant Connection](https://leetcode.com/problems/redundant-connection/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/RedundantConnection684.java) | Medium |
380381
| [685. Redundant Connection II](https://leetcode.com/problems/redundant-connection-ii/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/RedundantConnectionII685.java) | Hard |

src/BaseballGame682.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* You're now a baseball game point recorder.
3+
*
4+
* Given a list of strings, each string can be one of the 4 following types:
5+
*
6+
* Integer (one round's score): Directly represents the number of points you get in this round.
7+
* "+" (one round's score): Represents that the points you get in this round are the sum of the last two valid round's points.
8+
* "D" (one round's score): Represents that the points you get in this round are the doubled data of the last valid round's points.
9+
* "C" (an operation, which isn't a round's score): Represents the last valid round's points you get were invalid and should be removed.
10+
* Each round's operation is permanent and could have an impact on the round before and the round after.
11+
*
12+
* You need to return the sum of the points you could get in all the rounds.
13+
*
14+
* Example 1:
15+
* Input: ["5","2","C","D","+"]
16+
* Output: 30
17+
* Explanation:
18+
* Round 1: You could get 5 points. The sum is: 5.
19+
* Round 2: You could get 2 points. The sum is: 7.
20+
* Operation 1: The round 2's data was invalid. The sum is: 5.
21+
* Round 3: You could get 10 points (the round 2's data has been removed). The sum is: 15.
22+
* Round 4: You could get 5 + 10 = 15 points. The sum is: 30.
23+
*
24+
* Example 2:
25+
* Input: ["5","-2","4","C","D","9","+","+"]
26+
* Output: 27
27+
* Explanation:
28+
* Round 1: You could get 5 points. The sum is: 5.
29+
* Round 2: You could get -2 points. The sum is: 3.
30+
* Round 3: You could get 4 points. The sum is: 7.
31+
* Operation 1: The round 3's data is invalid. The sum is: 3.
32+
* Round 4: You could get -4 points (the round 3's data has been removed). The sum is: -1.
33+
* Round 5: You could get 9 points. The sum is: 8.
34+
* Round 6: You could get -4 + 9 = 5 points. The sum is 13.
35+
* Round 7: You could get 9 + 5 = 14 points. The sum is 27.
36+
*
37+
* Note:
38+
* The size of the input list will be between 1 and 1000.
39+
* Every integer represented in the list will be between -30000 and 30000.
40+
*/
41+
42+
public class BaseballGame682 {
43+
public int calPoints(String[] ops) {
44+
if (ops == null || ops.length == 0) return 0;
45+
Stack<Integer> stack = new Stack<>();
46+
int sum = 0;
47+
for (String op: ops) {
48+
if (op.equals("+")) {
49+
if (stack.size() >= 2) {
50+
int e1 = stack.pop();
51+
int e2 = stack.peek();
52+
int twoSum = e1 + e2;
53+
sum += twoSum;
54+
stack.push(e1);
55+
stack.push(twoSum);
56+
}
57+
} else if (op.equals("D")) {
58+
if (!stack.isEmpty()) {
59+
int doubledScore = stack.peek() * 2;
60+
sum += doubledScore;
61+
stack.push(doubledScore);
62+
}
63+
} else if (op.equals("C")) {
64+
if (!stack.isEmpty()) {
65+
int lastScore = stack.pop();
66+
sum -= lastScore;
67+
}
68+
} else {
69+
int now = Integer.parseInt(op);
70+
sum += now;
71+
stack.push(now);
72+
}
73+
}
74+
75+
return sum;
76+
}
77+
78+
}

0 commit comments

Comments
 (0)