确定第一个玩家是否可以赢得比赛
Determine if first player can win a game or not
Consider the following game. There's an array of positive numbers. Each player in turn removes a slice (consecutive sequence of elements) such that it's sum is even. The player to lose is the player that cannot make a move.
For the [4, 5, 3, 7, 2]
example, the winning strategy (for first player) is removing the[5,3]
slice.
You need to determine if the first player has a winning move (if yes, return the slice_start
and slice_end
indexes).
Time/Space complexity: O(n)
我想出的一个很自然的事情是从左到右计算累加和,反之亦然。在 [4,5,3,7,2]
示例中,我们得到:
acc_l = [4,9,12,19,21]
和 acc_r = [21,17,12,9,2]
.
所以我几乎可以肯定那两个辅助数组应该派上用场,但我无法完全弄清楚。
很乐意为您提供帮助!
计算整个数组的总和。检查它是偶数还是奇数。还要计算数组中偶数和奇数的数量(全部通过一次)。这应该可以帮助您找到答案。
Consider the following game. There's an array of positive numbers. Each player in turn removes a slice (consecutive sequence of elements) such that it's sum is even. The player to lose is the player that cannot make a move. For the
[4, 5, 3, 7, 2]
example, the winning strategy (for first player) is removing the[5,3]
slice. You need to determine if the first player has a winning move (if yes, return theslice_start
andslice_end
indexes).Time/Space complexity:
O(n)
我想出的一个很自然的事情是从左到右计算累加和,反之亦然。在 [4,5,3,7,2]
示例中,我们得到:
acc_l = [4,9,12,19,21]
和 acc_r = [21,17,12,9,2]
.
所以我几乎可以肯定那两个辅助数组应该派上用场,但我无法完全弄清楚。
很乐意为您提供帮助!
计算整个数组的总和。检查它是偶数还是奇数。还要计算数组中偶数和奇数的数量(全部通过一次)。这应该可以帮助您找到答案。