[LeetCode] 2660. Determine the Winner of a Bowling Game

发布时间 2023-12-27 06:56:10作者: CNoodle

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 hit 10 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, and
0 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 + 27 + 29 = 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 + 210 + 22 = 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

保龄球游戏的获胜者。

给你两个下标从 0 开始的整数数组 player1 和 player2 ,分别表示玩家 1 和玩家 2 击中的瓶数。

保龄球比赛由 n 轮组成,每轮的瓶数恰好为 10 。

假设玩家在第 i 轮中击中 xi 个瓶子。玩家第 i 轮的价值为:

如果玩家在该轮的前两轮的任何一轮中击中了 10 个瓶子,则为 2xi 。
否则,为 xi 。
玩家的得分是其 n 轮价值的总和。

返回

如果玩家 1 的得分高于玩家 2 的得分,则为 1 ;
如果玩家 2 的得分高于玩家 1 的得分,则为 2 ;
如果平局,则为 0 。

思路

按照题目的定义,我们计算一下每个玩家的得分。对于某一轮的分数,如果往前看一轮(或者两轮)是合法的且前一轮(或者前两轮)的分数是 10 分,那么当前轮的分数可以乘以 2。比较两个 player 谁的分数更高。简单模拟题意即可。

复杂度

时间O(n)
空间O(1)

代码

Java实现

class Solution {
    public int isWinner(int[] player1, int[] player2) {
        int n = player1.length;
        int score1 = 0;
        for (int i = 0; i < n; i++) {
            if (i - 1 >= 0 && player1[i - 1] == 10) {
                score1 += 2 * player1[i];
            } else if (i - 2 >= 0 && player1[i - 2] == 10) {
                score1 += 2 * player1[i];
            } else {
                score1 += player1[i];
            }
        }

        int score2 = 0;
        for (int i = 0; i < n; i++) {
            if (i - 1 >= 0 && player2[i - 1] == 10) {
                score2 += 2 * player2[i];
            } else if (i - 2 >= 0 && player2[i - 2] == 10) {
                score2 += 2 * player2[i];
            } else {
                score2 += player2[i];
            }
        }
        if (score1 > score2) {
            return 1;
        } else if (score1 < score2) {
            return 2;
        }
        return 0;
    }
}