2660. Determine the Winner of a Bowling Game LeetCode Solution
In this guide, you will get 2660. Determine the Winner of a Bowling Game LeetCode Solution with the best time and space complexity. The solution to Determine the Winner of a Bowling Game problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Determine the Winner of a Bowling Game solution in C++
- Determine the Winner of a Bowling Game solution in Java
- Determine the Winner of a Bowling Game solution in Python
- Additional Resources
Problem Statement of Determine the Winner of a Bowling Game
You are given two 0-indexed integer arrays player1 and player2, representing 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 hits xi pins in the ith turn. The value of the ith turn for the player is:
2xi if the player hits 10 pins in either (i – 1)th or (i – 2)th turn.
Otherwise, it is xi.
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 = [5,10,3,2], player2 = [6,5,7,3]
Output: 1
Explanation:
The score of player 1 is 5 + 10 + 2*3 + 2*2 = 25.
The score of player 2 is 6 + 5 + 7 + 3 = 21.
Example 2:
Input: player1 = [3,5,7,6], player2 = [8,10,10,2]
Output: 2
Explanation:
The score of player 1 is 3 + 5 + 7 + 6 = 21.
The score of player 2 is 8 + 10 + 2*10 + 2*2 = 42.
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.
Example 4:
Input: player1 = [1,1,1,10,10,10,10], player2 = [10,10,10,10,1,1,1]
Output: 2
Explanation:
The score of player1 is 1 + 1 + 1 + 10 + 2*10 + 2*10 + 2*10 = 73.
The score of player2 is 10 + 2*10 + 2*10 + 2*10 + 2*1 + 2*1 + 1 = 75.
Constraints:
n == player1.length == player2.length
1 <= n <= 1000
0 <= player1[i], player2[i] <= 10
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
2660. Determine the Winner of a Bowling Game LeetCode Solution in C++
class Solution {
public:
int isWinner(vector<int>& player1, vector<int>& player2) {
const int score1 = getScore(player1);
const int score2 = getScore(player2);
if (score1 > score2)
return 1;
if (score2 > score1)
return 2;
return 0;
}
private:
int getScore(const vector<int>& player) {
constexpr int kInvalid = -3;
int score = 0;
int last10 = kInvalid;
for (int i = 0; i < player.size(); ++i) {
score += i - last10 > 2 ? player[i] : player[i] * 2;
if (player[i] == 10)
last10 = i;
}
return score;
}
};
/* code provided by PROGIEZ */
2660. Determine the Winner of a Bowling Game LeetCode Solution in Java
class Solution {
public int isWinner(int[] player1, int[] player2) {
final int score1 = getScore(player1);
final int score2 = getScore(player2);
if (score1 > score2)
return 1;
if (score2 > score1)
return 2;
return 0;
}
private int getScore(int[] player) {
final int kInvalid = -3;
int score = 0;
int last10 = kInvalid;
for (int i = 0; i < player.length; ++i) {
score += i - last10 > 2 ? player[i] : player[i] * 2;
if (player[i] == 10)
last10 = i;
}
return score;
}
}
// code provided by PROGIEZ
2660. Determine the Winner of a Bowling Game LeetCode Solution in Python
class Solution:
def isWinner(self, player1: list[int], player2: list[int]) -> int:
def getScore(player: list[int]) -> int:
kInvalid = -3
score = 0
last10 = kInvalid
for i, p in enumerate(player):
score += p if i - last10 > 2 else p * 2
if p == 10:
last10 = i
return score
score1 = getScore(player1)
score2 = getScore(player2)
if score1 > score2:
return 1
if score2 > score1:
return 2
return 0
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.