Description#
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, 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
Solutions#
Solution 1: Simulation#
We can define a function $f(arr)$ to calculate the scores of the two players, denoted as $a$ and $b$, respectively, and then return the answer based on the relationship between $a$ and $b$.
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
1
2
3
4
5
6
7
8
9
10
11
| class Solution:
def isWinner(self, player1: List[int], player2: List[int]) -> int:
def f(arr: List[int]) -> int:
s = 0
for i, x in enumerate(arr):
k = 2 if (i and arr[i - 1] == 10) or (i > 1 and arr[i - 2] == 10) else 1
s += k * x
return s
a, b = f(player1), f(player2)
return 1 if a > b else (2 if b > a else 0)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| class Solution {
public int isWinner(int[] player1, int[] player2) {
int a = f(player1), b = f(player2);
return a > b ? 1 : b > a ? 2 : 0;
}
private int f(int[] arr) {
int s = 0;
for (int i = 0; i < arr.length; ++i) {
int k = (i > 0 && arr[i - 1] == 10) || (i > 1 && arr[i - 2] == 10) ? 2 : 1;
s += k * arr[i];
}
return s;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| class Solution {
public:
int isWinner(vector<int>& player1, vector<int>& player2) {
auto f = [](vector<int>& arr) {
int s = 0;
for (int i = 0, n = arr.size(); i < n; ++i) {
int k = (i && arr[i - 1] == 10) || (i > 1 && arr[i - 2] == 10) ? 2 : 1;
s += k * arr[i];
}
return s;
};
int a = f(player1), b = f(player2);
return a > b ? 1 : (b > a ? 2 : 0);
}
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| func isWinner(player1 []int, player2 []int) int {
f := func(arr []int) int {
s := 0
for i, x := range arr {
k := 1
if (i > 0 && arr[i-1] == 10) || (i > 1 && arr[i-2] == 10) {
k = 2
}
s += k * x
}
return s
}
a, b := f(player1), f(player2)
if a > b {
return 1
}
if b > a {
return 2
}
return 0
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| function isWinner(player1: number[], player2: number[]): number {
const f = (arr: number[]): number => {
let s = 0;
for (let i = 0; i < arr.length; ++i) {
s += arr[i];
if ((i && arr[i - 1] === 10) || (i > 1 && arr[i - 2] === 10)) {
s += arr[i];
}
}
return s;
};
const a = f(player1);
const b = f(player2);
return a > b ? 1 : a < b ? 2 : 0;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| impl Solution {
pub fn is_winner(player1: Vec<i32>, player2: Vec<i32>) -> i32 {
let f = |arr: &Vec<i32>| -> i32 {
let mut s = 0;
for i in 0..arr.len() {
let mut k = 1;
if (i > 0 && arr[i - 1] == 10) || (i > 1 && arr[i - 2] == 10) {
k = 2;
}
s += k * arr[i];
}
s
};
let a = f(&player1);
let b = f(&player2);
if a > b {
1
} else if a < b {
2
} else {
0
}
}
}
|