You are given an integer array player1
and an integer array player2
, each of length n
. The arrays represent the scores of two players in a bowling game across n
rounds. Each score is an integer between 1
and 10
, inclusive.
The game follows these rules:
Implement a function determine_winner(player1: List[int], player2: List[int]) -> int
that:
player1
and player2
.1
if player1 wins, 2
if player2 wins, and 0
if there is a tie.Let’s assume the input lists contain only the round score and not individual rolls, which means we only consider strikes for future round score doubling.
The solution will iterate through each list of scores once, making the time complexity O(n) where n is the number of rounds.
from typing import List
def determine_winner(player1: List[int], player2: List[int]) -> int:
def calculate_score(scores: List[int]) -> int:
total_score = 0
previous_was_strike = False
for i in range(len(scores)):
round_score = scores[i]
if previous_was_strike:
total_score += 2 * round_score
else:
total_score += round_score
if round_score == 10:
previous_was_strike = True
else:
previous_was_strike = False
return total_score
player1_total = calculate_score(player1)
player2_total = calculate_score(player2)
if player1_total > player2_total:
return 1
elif player2_total > player1_total:
return 2
else:
return 0
# Example usage:
# player1 = [10, 9, 8, 7, 6, 10, 10, 10, 10, 10]
# player2 = [10, 9, 8, 8, 9, 4, 6, 5, 10, 10]
# print(determine_winner(player1, player2)) # Output will depend on the total scores
All edge cases are considered, and we assume the simplest doubling rules. The function does not account for spares as individual roll data is not provided in problem statement. If spares should also affect the scores, then individual rolls must be known.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?