Determine the Winner of a Bowling Game solution and explanation
8 more videos on this problem
6341. Determine the Winner of a Bowling Game | Leetcode Weekly Contest 343 | Pseudo Code.Optimization · 4:58 · 893 views
Leetcode | 2660. Determine the Winner of a Bowling Game | Easy | Java SolutionDeveloper Docs · 8:42 · 857 views
Leetcode Weekly contest 343 - Easy - Determine the Winner of a Bowling GamePrakhar Agrawal · 7:33 · 408 views
Leetcode 2660. Determine the Winner of a Bowling Game | Weekly Contest 343 | Leetcode weekly contestOffCampus Phodenge | Aman Chowdhury · 7:32 · 368 views
2660. Determine the Winner of a Bowling Game (Leetcode Easy)Programming Live with Larry · 4:11 · 305 views
leetcode 2660 INDEXLAST10-3 PATTERN: winner of bowling game c++ solutionleetcode blind 75 · 4:01 · 196 views
Determine the Winner of a Bowling Game | LeetCode Weekly Contest 343 | Hindi | C++ SolutionUmer Karachiwala · 2:00 · 49 views
LeetCode 2660 | First Attempt Walkthrough & Narration | Determine the Winner of a Bowling GameDLG_Coding · 27:43 · 17 views
【LeetCode Life 7/50】2660. Determine the Winner of a Bowling Game (~Easy) MUST-DO思臉AI · 1:46 · 1 view
The problem
You are given two 0-indexed integer arrays player1 and player2, representing the number of pins that player 1 and player 2 hit in a bowling game, respectively.
The bowling game consists of n turns, and the number of pins in each turn is exactly 10.
Assume a player hits xi pins in the ith turn. The value of the ith turn for the player is:
2xiif the player hits 10 pins in either (i - 1)th or (i - 2)th turn.- Otherwise, it is
xi.
The score of the player is the sum of the values of their n turns.
Return
- 1 if the score of player 1 is more than the score of player 2,
- 2 if the score of player 2 is more than the score of player 1, and
- 0 in case of a draw.
Example 1:
Input: player1 = [5,10,3,2], player2 = [6,5,7,3]
Output: 1
Explanation:
The score of player 1 is 5 + 10 + 2*3 + 2*2 = 25.
The score of player 2 is 6 + 5 + 7 + 3 = 21.
Example 2:
Input: player1 = [3,5,7,6], player2 = [8,10,10,2]
Output: 2
Explanation:
The score of player 1 is 3 + 5 + 7 + 6 = 21.
The score of player 2 is 8 + 10 + 2*10 + 2*2 = 42.
Example 3:
Input: player1 = [2,3], player2 = [4,1]
Output: 0
Explanation:
The score of player1 is 2 + 3 = 5.
The score of player2 is 4 + 1 = 5.
Example 4:
Input: player1 = [1,1,1,10,10,10,10], player2 = [10,10,10,10,1,1,1]
Output: 2
Explanation:
The score of player1 is 1 + 1 + 1 + 10 + 2*10 + 2*10 + 2*10 = 73.
The score of player2 is 10 + 2*10 + 2*10 + 2*10 + 2*1 + 2*1 + 1 = 75.
Constraints:
n == player1.length == player2.length1 <= n <= 10000 <= player1[i], player2[i] <= 10
Approach overview
Problem Overview: You are given two arrays representing the pins knocked down by two bowling players in each round. A roll scores double if the player knocked down all 10 pins in either of the previous two rounds. Compute each player's total score and return the winner.
Approach 1: Simple Iterative Calculation (O(n) time, O(1) space)
This method directly simulates the scoring rule while iterating through the array. For every roll i, check whether either i-1 or i-2 equals 10. If so, the current roll contributes 2 * pins[i]; otherwise it contributes pins[i]. Accumulate the score in a running total. The same logic runs for both players and the final totals determine the winner.
The key insight is that the bonus rule depends only on the previous two rolls. No extra data structures are required, just boundary checks during iteration. This makes the solution extremely efficient and easy to implement. Problems like this are classic Array traversal combined with straightforward Simulation of rules.
Approach 2: Precompute Doubles (O(n) time, O(n) space)
Instead of checking the previous two rolls every time, precompute whether each index should receive a doubling bonus. Maintain a helper array or boolean flag indicating if the current roll is within the two-roll window after a strike. When a roll equals 10, mark the next two positions as doubled. During the final pass, compute the score using the precomputed flags.
This separates rule processing from score calculation, which some developers find cleaner when the scoring logic grows more complex. It trades constant memory for clarity, since the bonus information is stored explicitly. The algorithm still performs a single linear scan, keeping the time complexity at O(n).
Recommended for interviews: The simple iterative calculation is what interviewers usually expect. It demonstrates that you can translate a rule-based scoring system into efficient code using a single pass. Implementing the precompute approach shows the same understanding but uses more memory. Start with the direct simulation since it is shorter, clearer, and runs in O(n) time with O(1) space.
Approach 1: Simple Iterative Calculation
This approach involves iterating through the array of scores for each player, calculating the score for each turn while checking the previous two turn scores. If any of the last two turns resulted in 10 pins, the score for the current turn is doubled. We maintain a running total of scores.
def calculate_score(player):
n = len(player)
score = 0
for i in range(n):
if (i > 0 and player[i-1] == 10) or (i > 1 and player[i-2] == 10):
score += 2 * player[i]
else:
score += player[i]
return score
def bowling_winner(player1, player2):
score1 = calculate_score(player1)
score2 = calculate_score(player2)
if score1 > score2:
return 1
elif score2 > score1:
return 2
else:
return 0#include <vector>
using namespace std;
int calculateScore(vector<int>& player) {
int n = player.size();
int score = 0;
for (int i = 0; i < n; ++i) {
if ((i > 0 && player[i-1] == 10) || (i > 1 && player[i-2] == 10)) {
score += 2 * player[i];
} else {
score += player[i];
}
}
return score;
}
int bowlingWinner(vector<int>& player1, vector<int>& player2) {
int score1 = calculateScore(player1);
int score2 = calculateScore(player2);
if (score1 > score2) return 1;
else if (score2 > score1) return 2;
else return 0;
}In this Python solution, we define a helper function calculate_score that calculates the total score of a player based on the rules described. We check the scores in the previous two indices. If any of them is 10, we double the current score. Finally, we compare the total scores of both players to determine the winner.
Time Complexity: O(n), where n is the length of the player score array.
Space Complexity: O(1), as we only use a fixed amount of extra space.
Approach 2: Precompute Doubles
In this approach, we first precompute an array indicating whether each turn's score should be doubled. This separates the logic of scoring from the main loop, potentially making the implementation more readable.
public class BowlingGame {
private static int calculateScore(int[] player) {
int n = player.length;
int score = 0;
boolean[] isDouble = new boolean[n];
for (int i = 0; i < n; i++) {
if (i > 0 && player[i-1] == 10) isDouble[i] = true;
if (i > 1 && player[i-2] == 10) isDouble[i] = true;
}
for (int i = 0; i < n; i++) {
score += isDouble[i] ? 2 * player[i] : player[i];
}
return score;
}
public static int bowlingWinner(int[] player1, int[] player2) {
int score1 = calculateScore(player1);
int score2 = calculateScore(player2);
if (score1 > score2) {
return 1;
} else if (score2 > score1) {
return 2;
} else {
return 0;
}
}
}function calculateScore(player) {
const n = player.length;
let score = 0;
const isDouble = new Array(n).fill(false);
for (let i = 0; i < n; i++) {
if (i > 0 && player[i-1] == 10) isDouble[i] = true;
if (i > 1 && player[i-2] == 10) isDouble[i] = true;
}
for (let i = 0; i < n; i++) {
score += isDouble[i] ? 2 * player[i] : player[i];
}
return score;
}
function bowlingWinner(player1, player2) {
const score1 = calculateScore(player1);
const score2 = calculateScore(player2);
if (score1 > score2) {
return 1;
} else if (score2 > score1) {
return 2;
} else {
return 0;
}
}In this Java solution, we first determine which turns will have their scores doubled in a separate boolean array isDouble. Using this information, we determine the actual score for each player. This slightly separates the logic into computing isDouble values first, which may improve code readability.
Time Complexity: O(n), where n is the length of the player score array.
Space Complexity: O(n), due to the extra storage required for the isDouble array.
Approach 3: Simulation
We can define a function f(arr) to calculate the scores of the two players, denoted as a and b, respectively, and then return the answer based on the relationship between a and b.
The time complexity is O(n), where n is the length of the array. The space complexity is O(1).
function isWinner(player1: number[], player2: number[]): number {
const f = (arr: number[]): number => {
let s = 0;
for (let i = 0; i < arr.length; ++i) {
s += arr[i];
if ((i && arr[i - 1] === 10) || (i > 1 && arr[i - 2] === 10)) {
s += arr[i];
}
}
return s;
};
const a = f(player1);
const b = f(player2);
return a > b ? 1 : a < b ? 2 : 0;
}func isWinner(player1 []int, player2 []int) int {
f := func(arr []int) int {
s := 0
for i, x := range arr {
k := 1
if (i > 0 && arr[i-1] == 10) || (i > 1 && arr[i-2] == 10) {
k = 2
}
s += k * x
}
return s
}
a, b := f(player1), f(player2)
if a > b {
return 1
}
if b > a {
return 2
}
return 0
}impl Solution {
pub fn is_winner(player1: Vec<i32>, player2: Vec<i32>) -> i32 {
let f = |arr: &Vec<i32>| -> i32 {
let mut s = 0;
for i in 0..arr.len() {
let mut k = 1;
if (i > 0 && arr[i - 1] == 10) || (i > 1 && arr[i - 2] == 10) {
k = 2;
}
s += k * arr[i];
}
s
};
let a = f(&player1);
let b = f(&player2);
if a > b {
1
} else if a < b {
2
} else {
0
}
}
}Complexity comparison
| Approach | Complexity |
|---|---|
| Simple Iterative Calculation | Time Complexity: O(n), where n is the length of the player score array. |
| Precompute Doubles | Time Complexity: O(n), where n is the length of the player score array. |
| Simulation | – |
Complexity analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Simple Iterative Calculation | O(n) | O(1) | Best general solution. Minimal memory and straightforward rule simulation. |
| Precompute Doubles | O(n) | O(n) | Useful when separating rule tracking from scoring logic for readability or extension. |
Frequently asked questions
Is Determine the Winner of a Bowling Game easy or hard?
LeetCode classifies this problem as Easy. The main challenge is correctly applying the bonus rule when a player scores a strike and ensuring the previous two rolls are checked safely during iteration.
Determine the Winner of a Bowling Game Python/Java solution
In Python or Java, iterate through the roll array and compute the score using conditional checks on the previous two indices. Maintain a running total and compare both players' scores at the end. The implementation is typically under 15 lines of code.
How to solve Determine the Winner of a Bowling Game in O(n)?
Iterate through the rolls and maintain a running score. For each index i, check if rolls[i-1] == 10 or rolls[i-2] == 10. If either condition holds, add 2 * rolls[i] to the score; otherwise add rolls[i]. Repeat for both players and compare the totals.
What is the best approach for Determine the Winner of a Bowling Game?
The best approach is a single-pass iterative simulation. Traverse each player's array and check whether either of the previous two rolls equals 10. If so, double the current roll's score. This method runs in O(n) time and uses O(1) extra space.
Is Determine the Winner of a Bowling Game asked at Google/Amazon/Meta?
This specific problem is categorized as an easy array simulation question. Similar rule-simulation or scoring problems appear in interviews at large tech companies where candidates must convert problem rules into efficient code.
What data structure is used in Determine the Winner of a Bowling Game?
The core data structure is an array representing the pins knocked down in each round. The algorithm processes the array sequentially and may optionally use an auxiliary array when precomputing double-score rounds.
What is the time complexity of Determine the Winner of a Bowling Game?
The optimal solution runs in O(n) time where n is the number of rolls. Each roll is processed once while checking at most two previous indices. Space complexity is O(1) because only running totals are stored.
