[LeetCode] Determine the Winner of a Bowling Game

2660. Determine the Winner of a Bowling Game

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
int isWinner(vector<int>& player1, vector<int>& player2) {
int a = 0, b = 0;
int fl1 = 1, fl2 = 1;
for(int i = 0; i < player1.size(); i++) {
if((i and player1[i-1] == 10) or (i >= 2 and player1[i-2] == 10)) a += player1[i] *2 ;
else a += player1[i];
}
for(int i = 0; i < player2.size(); i++) {
if((i and player2[i-1] == 10) or (i >= 2 and player2[i-2] == 10)) b += player2[i] * 2;
else b += player2[i];
}
return a > b ? 1 : a < b ? 2 : 0;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/04/30/PS/LeetCode/determine-the-winner-of-a-bowling-game/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.