#2660
Easy Algorithms Determine the winner of a bowling game
Array Simulation
37.2% acceptance
Feb 25, 2026
293
157
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:
2xi if 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 player 1 score > player 2, 2 if player 2 > player 1, and 0 if draw.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn is_winner(player1: Vec<i32>, player2: Vec<i32>) -> i32 {
let score = |p: &Vec<i32>| -> i32 {
let n = p.len();
let mut s = 0;
for i in 0..n {
let bonus = (i >= 1 && p[i - 1] == 10) || (i >= 2 && p[i - 2] == 10);
s += if bonus { 2 * p[i] } else { p[i] };
}
s
};
let s1 = score(&player1);
let s2 = score(&player2);
if s1 > s2 { 1 } else if s2 > s1 { 2 } else { 0 }
}
}