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:

  • 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 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.

Examples

Example 1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22

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.length
  • 1 <= n <= 1000
  • 0 <= player1[i], player2[i] <= 10

Solution

Method 1 – Simulation with Bonus Tracking

Intuition

We need to simulate the scoring for each player, keeping track of whether the previous one or two turns were strikes (10 pins). If so, the current turn’s score is doubled. We sum up the scores for both players and compare.

Approach

  1. For each player, initialize a score and a list to track the last two turns.
  2. For each turn, check if either of the last two turns was a strike (10 pins). If so, double the current turn’s score.
  3. Add the score to the total and update the last two turns.
  4. After both scores are computed, compare and return 1, 2, or 0 as required.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
    int calc(vector<int>& arr) {
        int n = arr.size(), ans = 0;
        int last1 = 0, last2 = 0;
        for (int i = 0; i < n; ++i) {
            int v = arr[i];
            if (last1 == 10 || last2 == 10) v *= 2;
            ans += v;
            last2 = last1;
            last1 = arr[i];
        }
        return ans;
    }
    int isWinner(vector<int>& p1, vector<int>& p2) {
        int s1 = calc(p1), s2 = calc(p2);
        if (s1 > s2) return 1;
        if (s2 > s1) return 2;
        return 0;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
func calc(arr []int) int {
    n := len(arr)
    ans, last1, last2 := 0, 0, 0
    for i := 0; i < n; i++ {
        v := arr[i]
        if last1 == 10 || last2 == 10 {
            v *= 2
        }
        ans += v
        last2 = last1
        last1 = arr[i]
    }
    return ans
}
func isWinner(p1, p2 []int) int {
    s1, s2 := calc(p1), calc(p2)
    if s1 > s2 {
        return 1
    }
    if s2 > s1 {
        return 2
    }
    return 0
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution {
    private int calc(int[] arr) {
        int n = arr.length, ans = 0, last1 = 0, last2 = 0;
        for (int i = 0; i < n; ++i) {
            int v = arr[i];
            if (last1 == 10 || last2 == 10) v *= 2;
            ans += v;
            last2 = last1;
            last1 = arr[i];
        }
        return ans;
    }
    public int isWinner(int[] p1, int[] p2) {
        int s1 = calc(p1), s2 = calc(p2);
        if (s1 > s2) return 1;
        if (s2 > s1) return 2;
        return 0;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
    private fun calc(arr: IntArray): Int {
        var ans = 0
        var last1 = 0
        var last2 = 0
        for (a in arr) {
            var v = a
            if (last1 == 10 || last2 == 10) v *= 2
            ans += v
            last2 = last1
            last1 = a
        }
        return ans
    }
    fun isWinner(p1: IntArray, p2: IntArray): Int {
        val s1 = calc(p1)
        val s2 = calc(p2)
        return when {
            s1 > s2 -> 1
            s2 > s1 -> 2
            else -> 0
        }
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
class Solution:
    def calc(self, arr: list[int]) -> int:
        ans = 0
        last1 = last2 = 0
        for a in arr:
            v = a
            if last1 == 10 or last2 == 10:
                v *= 2
            ans += v
            last2 = last1
            last1 = a
        return ans
    def isWinner(self, p1: list[int], p2: list[int]) -> int:
        s1 = self.calc(p1)
        s2 = self.calc(p2)
        if s1 > s2:
            return 1
        if s2 > s1:
            return 2
        return 0
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
impl Solution {
    pub fn is_winner(p1: Vec<i32>, p2: Vec<i32>) -> i32 {
        fn calc(arr: &Vec<i32>) -> i32 {
            let mut ans = 0;
            let mut last1 = 0;
            let mut last2 = 0;
            for &a in arr {
                let mut v = a;
                if last1 == 10 || last2 == 10 {
                    v *= 2;
                }
                ans += v;
                last2 = last1;
                last1 = a;
            }
            ans
        }
        let s1 = calc(&p1);
        let s2 = calc(&p2);
        if s1 > s2 { 1 } else if s2 > s1 { 2 } else { 0 }
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
    private calc(arr: number[]): number {
        let ans = 0, last1 = 0, last2 = 0;
        for (const a of arr) {
            let v = a;
            if (last1 === 10 || last2 === 10) v *= 2;
            ans += v;
            last2 = last1;
            last1 = a;
        }
        return ans;
    }
    isWinner(p1: number[], p2: number[]): number {
        const s1 = this.calc(p1);
        const s2 = this.calc(p2);
        if (s1 > s2) return 1;
        if (s2 > s1) return 2;
        return 0;
    }
}

Complexity

  • ⏰ Time complexity: O(n), where n is the number of turns. We process each turn once for each player.
  • 🧺 Space complexity: O(1), as only a few variables are used for tracking state.