Determine the Winner of a Bowling Game - Problem
Bowling Championship Simulator
Welcome to the digital bowling alley! ๐ณ You're tasked with determining the winner of a special bowling game between two players.
In this unique bowling variant:
โข Each game consists of
โข Each turn has exactly 10 pins available
โข Players can hit anywhere from
The Scoring Twist: If a player hits 10 pins (a strike) in either the previous turn
Your Mission: Calculate both players' total scores and determine:
โข Return
โข Return
โข Return
Can you build the perfect scoring system? ๐
Welcome to the digital bowling alley! ๐ณ You're tasked with determining the winner of a special bowling game between two players.
In this unique bowling variant:
โข Each game consists of
n turnsโข Each turn has exactly 10 pins available
โข Players can hit anywhere from
0 to 10 pins per turnThe Scoring Twist: If a player hits 10 pins (a strike) in either the previous turn
(i-1) or two turns ago (i-2), their current turn's score gets doubled!Your Mission: Calculate both players' total scores and determine:
โข Return
1 if Player 1 winsโข Return
2 if Player 2 winsโข Return
0 if it's a tieCan you build the perfect scoring system? ๐
Input & Output
example_1.py โ Basic Strike Bonus
$
Input:
player1 = [4,10,7,9], player2 = [6,5,10,4]
โบ
Output:
1
๐ก Note:
Player 1 scores: 4 + 10 + (7ร2) + (9ร2) = 46. Player 2 scores: 6 + 5 + 10 + (4ร2) = 29. Player 1 wins!
example_2.py โ Equal Scores
$
Input:
player1 = [3,5,7,6], player2 = [8,10,10,2]
โบ
Output:
2
๐ก Note:
Player 1 scores: 3 + 5 + 7 + 6 = 21. Player 2 scores: 8 + 10 + 10 + (2ร2) = 32. Player 2 wins!
example_3.py โ Two Turns Back Strike
$
Input:
player1 = [2,3], player2 = [4,1]
โบ
Output:
0
๐ก Note:
Player 1 scores: 2 + 3 = 5. Player 2 scores: 4 + 1 = 5. It's a tie!
Visualization
Tap to expand
Understanding the Visualization
1
Track Each Turn
Process each player's turns sequentially
2
Check Strike History
Look back 1-2 turns for strikes (10 pins)
3
Apply Bonus
Double current score if recent strikes found
4
Determine Winner
Compare final totals
Key Takeaway
๐ฏ Key Insight: Track the last two turns to efficiently apply strike bonuses in O(n) time - no complex data structures needed!
Time & Space Complexity
Time Complexity
O(n)
We iterate through each turn once, checking previous turns takes constant time
โ Linear Growth
Space Complexity
O(1)
Only using variables to track scores, no additional data structures
โ Linear Space
Constraints
- n == player1.length == player2.length
- 1 โค n โค 1000
- 0 โค player1[i], player2[i] โค 10
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code