Skip to content

Commit cda5db8

Browse files
solves #2660: Determine the Winner of a Bowling Game in java
1 parent f90e94a commit cda5db8

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,6 @@
819819
| 2651 | [Calculate Delayed Arrival Time](https://leetcode.com/problems/calculate-delayed-arrival-time) | [![Java](assets/java.png)](src/CalculateDelayedArrivalTime.java) | |
820820
| 2652 | [Sum Multiples](https://leetcode.com/problems/sum-multiples) | [![Java](assets/java.png)](src/SumMultiples.java) | |
821821
| 2656 | [Maximum Sum With Exactly K Elements](https://leetcode.com/problems/maximum-sum-with-exactly-k-elements) | [![Java](assets/java.png)](src/MaximumSumWithExactlyKElements.java) | |
822-
| 2660 | [Determine the Winner of a Bowling Game](https://leetcode.com/problems/determine-the-winner-of-a-bowling-game) | | |
822+
| 2660 | [Determine the Winner of a Bowling Game](https://leetcode.com/problems/determine-the-winner-of-a-bowling-game) | [![Java](assets/java.png)](src/DetermineTheWinnerOfABowlingGame.java) | |
823823
| 2670 | [Find the Distinct Difference Array](https://leetcode.com/problems/find-the-distinct-difference-array) | | |
824824
| 2678 | [Number of Senior Citizens](https://leetcode.com/problems/number-of-senior-citizens) | | |
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// https://leetcode.com/problems/determine-the-winner-of-a-bowling-game
2+
// T: O(n)
3+
// S: O(1)
4+
5+
public class DetermineTheWinnerOfABowlingGame {
6+
public int isWinner(int[] player1, int[] player2) {
7+
final int score1 = getPlayerScore(player1);
8+
final int score2 = getPlayerScore(player2);
9+
if (score1 > score2) return 1;
10+
if (score1 < score2) return 2;
11+
return 0;
12+
}
13+
14+
private int getPlayerScore(int[] scores) {
15+
int total = 0, seen10 = 0;
16+
for (int score : scores) {
17+
if (seen10 > 0) {
18+
total += 2 * score;
19+
seen10--;
20+
} else {
21+
total += score;
22+
}
23+
if (score == 10) {
24+
seen10 = 2;
25+
}
26+
}
27+
return total;
28+
}
29+
}

0 commit comments

Comments
 (0)