【每日一题】保龄球游戏的获胜者

文章目录

  • Tag
  • 题目来源
  • 解题思路
    • 方法一:模拟
  • 写在最后

Tag

【模拟】【数组】【2023-12-27】


题目来源

2660. 保龄球游戏的获胜者

【每日一题】保龄球游戏的获胜者_第1张图片

解题思路

方法一:模拟

思路

本题较为简单,按照题目思路模拟即可。

遍历每一轮的击中的瓶子数,如果该轮的前两轮中有任何一轮击中了 10 个瓶子,则该轮的得分为瓶子数的二倍,否则该轮的得分即为瓶子数。

算法

class Solution {
public:
    int sumScore(vector<int>& player) {
        int sum = 0;
        for (int i = 0; i < player.size(); ++i) {
            if (i - 1 >= 0 && i -2 >= 0) {
                if (player[i-1] == 10 || player[i-2] == 10) {
                    sum += 2 * player[i];
                }
            }
            else {
                sum += player[i];
            }
        }
        return sum;
    }
    
    int isWinner(vector<int>& player1, vector<int>& player2) {
        int sum1, sum2;
        sum1 = sumScore(player1);
        sum2 = sumScore(player2);
        return sum1 == sum2 ? 0 : (sum1 > sum2 ? 1 : 2);
    }
};

复杂度分析

时间复杂度: O ( n ) O(n) O(n) n n n 为本题中整数数组的长度。

空间复杂度: O ( 1 ) O(1) O(1)


写在最后

如果您发现文章有任何错误或者对文章有任何疑问,欢迎私信博主或者在评论区指出 。

如果大家有更优的时间、空间复杂度的方法,欢迎评论区交流。

最后,感谢您的阅读,如果有所收获的话可以给我点一个 哦。

你可能感兴趣的:(LeetCode每日一题,模拟,数组,2023-12-27,C++)