algoadvance

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:

Clarifying Questions

  1. How is a spare represented in the input arrays?
  2. If we don’t have information on spares explicitly, should we assume only strikes contribute to future score doubling?
  3. Are the additional rules (like doubling scores in subsequent rounds) applied cumulatively, i.e., can multiple rounds of doubling be stacked?

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.

Strategy

  1. Tracking Scores & Doubling: Each player has their own list of scores for each round.
  2. Score Calculation:
    • Iterate through each round and sum up the scores.
    • Track if the previous round was a strike to decide if the next round’s score should be doubled.
  3. Determine Winner:
    • Compare the total scores after all the rounds are summed up considering the doubling criteria.

Time Complexity

The solution will iterate through each list of scores once, making the time complexity O(n) where n is the number of rounds.

Code

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.

Try our interview co-pilot at AlgoAdvance.com