Leetcode 2660: Determine the Winner of a Bowling Game

grid47
grid47
Exploring patterns and algorithms
Feb 15, 2024 7 min read

In a bowling game, two players take turns to hit pins. Each player hits a certain number of pins per turn, and the total score is calculated based on how many pins they hit and whether they hit 10 pins in previous turns. If a player hits 10 pins in one of the previous two turns, their score for the current turn is doubled. The objective is to determine which player has the higher score after all turns have been played.
Problem
Approach
Steps
Complexity
Input: You are given two integer arrays, player1 and player2, where each element represents the number of pins hit by the corresponding player in each turn.
Example: Input: player1 = [3,10,2,5], player2 = [4,6,8,2]
Constraints:
• 1 <= n <= 1000
• 0 <= player1[i], player2[i] <= 10
Output: Return 1 if player 1 has a higher score, 2 if player 2 has a higher score, and 0 in case of a draw.
Example: Output: 1
Constraints:
• The output should be either 1, 2, or 0 based on the comparison of the scores.
Goal: The goal is to calculate the total score for both players and compare them to determine the winner.
Steps:
• Step 1: For each player, iterate through their scores for each turn.
• Step 2: If the player hits 10 pins in the previous turn or the one before that, double the score for the current turn.
• Step 3: Sum all the scores for each player.
• Step 4: Compare the total scores and return the appropriate result based on which player's score is higher.
Goal: The grid must be processed efficiently given the maximum possible length of the arrays.
Steps:
• The number of turns (n) is up to 1000, so the solution should work in linear time.
Assumptions:
• The number of turns for both players is the same.
Input: Input: player1 = [3,10,2,5], player2 = [4,6,8,2]
Explanation: Player 1's score is 3 + 10 + 2*2 + 5 = 25, and Player 2's score is 4 + 6 + 8 + 2 = 20. Therefore, Player 1 wins.

Input: Input: player1 = [5,1,4,10], player2 = [2,10,2,8]
Explanation: Player 1's score is 5 + 1 + 4 + 2*10 = 25, and Player 2's score is 2 + 10 + 2*2 + 8 = 24. Player 1 wins.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus