保龄球游戏的获胜者
难度:
标签:
题目描述
You are given two 0-indexed integer arrays player1
and player2
, that represent 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 hit xi
pins in the ith
turn. The value of the ith
turn for the player is:
2xi
if the player hit10
pins in any of the previous two turns.- 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, and0
in case of a draw.
Example 1:
Input: player1 = [4,10,7,9], player2 = [6,5,2,3] Output: 1 Explanation: The score of player1 is 4 + 10 + 2*7 + 2*9 = 46. The score of player2 is 6 + 5 + 2 + 3 = 16. Score of player1 is more than the score of player2, so, player1 is the winner, and the answer is 1.
Example 2:
Input: player1 = [3,5,7,6], player2 = [8,10,10,2] Output: 2 Explanation: The score of player1 is 3 + 5 + 7 + 6 = 21. The score of player2 is 8 + 10 + 2*10 + 2*2 = 42. Score of player2 is more than the score of player1, so, player2 is the winner, and the answer is 2.
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 The score of player1 equals to the score of player2, so, there is a draw, and the answer is 0.
Constraints:
n == player1.length == player2.length
1 <= n <= 1000
0 <= player1[i], player2[i] <= 10
代码结果
运行时间: 32 ms, 内存: 16.5 MB
/*
思路:
1. 使用Java Stream API来计算每个玩家的总得分。
2. 使用一个IntStream来遍历数组,并使用reduce方法计算总得分。
3. 在计算得分时,检查前两轮的击中瓶数,如果任何一轮的瓶数为10,则当前轮的击中瓶数乘以2。
4. 比较两个玩家的总得分,返回结果。
*/
import java.util.stream.IntStream;
public class BowlingGameStream {
public static int findWinner(int[] player1, int[] player2) {
int score1 = calculateScore(player1);
int score2 = calculateScore(player2);
return Integer.compare(score1, score2);
}
private static int calculateScore(int[] player) {
return IntStream.range(0, player.length)
.map(i -> {
if (i >= 2 && (player[i - 1] == 10 || player[i - 2] == 10)) {
return 2 * player[i];
} else {
return player[i];
}
})
.sum();
}
public static void main(String[] args) {
int[] player1 = {4, 10, 7, 9};
int[] player2 = {6, 5, 2, 3};
System.out.println(findWinner(player1, player2)); // 输出: 1
}
}
解释
方法:
此题解的思路是逐轮计算两位玩家的得分。如果当前轮次大于1(即i>1),则检查前两轮中是否有任意一轮得分为10,若有,则当前轮得分翻倍;如果当前轮次为第二轮(i=1),则只需检查第一轮的得分;如果是第一轮(i=0),直接加当前得分。最后,比较两位玩家的总分,根据得分情况返回胜者。
时间复杂度:
O(n)
空间复杂度:
O(1)
代码细节讲解
🦆
如何处理玩家在两轮连续击中10个瓶子的情况?是否有额外的得分规则适用于这种连续得分?
▷🦆
如果数组`player1`和`player2`长度不相等,该如何处理?题解中是否假设了两个数组长度总是相同?
▷🦆
在计算得分时,为何没有考虑连续三轮或更多轮击中10个瓶子的加分规则?这是否遗漏了部分可能的得分情况?
▷🦆
代码中使用了`i > 1`来判断是否查看前两轮的得分,这种方法是否能有效处理所有边界情况,特别是数组长度少于3的情况?
▷