LeetCode 2660: Determine the Winner of a Bowling Game Solution
Master LeetCode problem 2660 (Determine the Winner of a Bowling Game), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
2660. Determine the Winner of a Bowling Game
Problem Explanation
Explanation
To solve this problem, we need to calculate the scores of both players based on the given rules and compare them to determine the winner. We can iterate through each turn for both players, calculate the score for each turn according to the rules provided, and then sum up the scores to get the total score for each player. Finally, we compare the total scores to determine the winner.
Algorithm:
- Initialize variables to store the total scores of player 1 and player 2.
- Iterate through each turn for both players.
- For each turn, calculate the score based on the rules provided.
- Sum up the scores for each player to get the total score.
- Compare the total scores to determine the winner.
Time Complexity:
The time complexity of this algorithm is O(n), where n is the number of turns in the game.
Space Complexity:
The space complexity of this algorithm is O(1) as we are using a constant amount of extra space regardless of the input size.
Solution Code
class Solution {
public int determineWinner(int[] player1, int[] player2) {
int scorePlayer1 = 0, scorePlayer2 = 0;
for (int i = 0; i < player1.length; i++) {
int turnScore1 = calculateTurnScore(player1, i);
int turnScore2 = calculateTurnScore(player2, i);
scorePlayer1 += turnScore1;
scorePlayer2 += turnScore2;
}
if (scorePlayer1 > scorePlayer2) {
return 1;
} else if (scorePlayer2 > scorePlayer1) {
return 2;
} else {
return 0;
}
}
private int calculateTurnScore(int[] player, int index) {
if (index >= 2 && player[index] == 10 && (player[index - 1] == 10 || player[index - 2] == 10)) {
return 2 * player[index];
} else {
return player[index];
}
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 2660 (Determine the Winner of a Bowling Game)?
This page provides optimized solutions for LeetCode problem 2660 (Determine the Winner of a Bowling Game) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 2660 (Determine the Winner of a Bowling Game)?
The time complexity for LeetCode 2660 (Determine the Winner of a Bowling Game) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 2660 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2660 in Java, C++, or Python.