You are given two 0-indexed integer arrays player1 and player2, representing 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 hits xi pins in the ith turn. The value of the ith turn for the player is:
2xi if the player hits 10 pins in either (i - 1) th or (i - 2)th turn.
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
Input: player1 =[2,3], player2 =[4,1]Output: 0Explanation:
The score of player1 is2+3=5.The score of player2 is4+1=5.#### Example 4Input: player1 =[1,1,1,10,10,10,10], player2 =[10,10,10,10,1,1,1]Output: 2Explanation:
The score of player1 is1+1+1+10+2*10+2*10+2*10=73.The score of player2 is10+2*10+2*10+2*10+2*1+2*1+1=75.
We need to simulate the scoring for each player, keeping track of whether the previous one or two turns were strikes (10 pins). If so, the current turn’s score is doubled. We sum up the scores for both players and compare.
classSolution {
public:int calc(vector<int>& arr) {
int n = arr.size(), ans =0;
int last1 =0, last2 =0;
for (int i =0; i < n; ++i) {
int v = arr[i];
if (last1 ==10|| last2 ==10) v *=2;
ans += v;
last2 = last1;
last1 = arr[i];
}
return ans;
}
intisWinner(vector<int>& p1, vector<int>& p2) {
int s1 = calc(p1), s2 = calc(p2);
if (s1 > s2) return1;
if (s2 > s1) return2;
return0;
}
};
classSolution {
privatefuncalc(arr: IntArray): Int {
var ans = 0var last1 = 0var last2 = 0for (a in arr) {
var v = a
if (last1 ==10|| last2 ==10) v *=2 ans += v
last2 = last1
last1 = a
}
return ans
}
funisWinner(p1: IntArray, p2: IntArray): Int {
val s1 = calc(p1)
val s2 = calc(p2)
returnwhen {
s1 > s2 ->1 s2 > s1 ->2else->0 }
}
}
classSolution:
defcalc(self, arr: list[int]) -> int:
ans =0 last1 = last2 =0for a in arr:
v = a
if last1 ==10or last2 ==10:
v *=2 ans += v
last2 = last1
last1 = a
return ans
defisWinner(self, p1: list[int], p2: list[int]) -> int:
s1 = self.calc(p1)
s2 = self.calc(p2)
if s1 > s2:
return1if s2 > s1:
return2return0