2660. Determine the Winner of a Bowling Game

You are given two 0-indexed integer arrays player1 and player2, that represent 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 hit x[i] pins in the ith turn. The value of the ith turn for the player is:
    - 2 * x[i] if the player hit 10 pins in any of the previous two turns.
    - Otherwise, it is x[i].

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 = [4,10,7,9], player2 = [6,5,2,3]
Output: 1
Explanation: The score of player1 is 4 + 10 + 2*7 + 2*9 = 46.
The score of player2 is 6 + 5 + 2 + 3 = 16.
Score of player1 is more than the score of player2, so, player1 is the winner, and the answer is 1.

Example 2:
Input: player1 = [3,5,7,6], player2 = [8,10,10,2]
Output: 2
Explanation: The score of player1 is 3 + 5 + 7 + 6 = 21.
The score of player2 is 8 + 10 + 2*10 + 2*2 = 42.
Score of player2 is more than the score of player1, so, player2 is the winner, and the answer is 2.

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
The score of player1 equals to the score of player2, so, there is a draw, and the answer is 0.

Constraints:
    n == player1.length == player2.length
    1 <= n <= 1000
    0 <= player1[i], player2[i] <= 10

https://leetcode.com/problems/determine-the-winner-of-a-bowling-game/


Video coming soon!



Subscribe for more updates


Algorithm/Insights

In a bowling game, the player with the highest score wins. The score of a player depends on the number of pins they hit in each turn. If a player hits 10 pins in any of the previous two turns, the value of their current turn is doubled. Otherwise, the value of their current turn is equal to the number of pins hit in that turn.

To determine the winner of a bowling game given the number of pins hit by two players in each turn, we need to calculate the total score of each player and compare them. We can do this by iterating through each turn of the game and applying the scoring rules.

Algorithm:
    1. Initialize two variables, score1 and score2, to 0.
    2. Iterate through each turn of the game using a loop variable i from 0 to n-1, where n is the number of turns in the game.
    3. For each turn, calculate the score of player 1 and player 2 using the findScore method and add it to their respective scores.
    4. Compare the scores of the two players. If they are equal, return 0. Otherwise, return 1 if score1 is greater than score2 and 2 if score2 is greater than score1.

The findScore method calculates the score of a player in a given turn based on the scoring rules.


Code Snippet

			
package com.ideserve.sakshi.questions;

/**
 * <b>IDeserve <br>
 * <a href="https://www.youtube.com/c/IDeserve">https://www.youtube.com/c/IDeserve</a>
 * Determine the Winner of a Bowling Game
 *
 * @author Sakshi
 */
class Solution {
    public int isWinner(int[] player1, int[] player2) {
        int n = player1.length;
        int score1 = 0;
        int score2 = 0;
        for (int i = 0; i < n; i++) {
            score1 += findScore(player1, i);
            score2 += findScore(player2, i);
        }
        
        if (score1 == score2) {
            return 0;
        }

        return score1 > score2 ? 1 : 2;
    }

    public int findScore(int[] player, int i) {
        if ((i > 0 && player[i-1] == 10) || (i > 1 && player[i-2] == 10)) {
            return 2 * player[i];
        } else {
            return player[i];
        }
    }
}
		

Order of the Algorithm

Time Complexity is O(n)
Space Complexity is O(1)


Contribution

  • Sincere thanks from IDeserve community to Sakshi Kataria for compiling current post.


    Sakshi Kataria