A video game developer is developing a game in which the character makes their way
through several segments of a level. In each segment, if the character collects a coin,
the player scores a point. However, if a segment does not contain a coin, the player
loses a point. Player 1
always begins the level, and, at some point, game play is turned over
to Player 2
to complete the level. Player 1
's goal is to achieve a higher score than Player 2
once the level is completed. Given the status of game segments (whether they contain a coin or not),
determine the minimum number of segments Player 1
should play so that, in the end, their
score is greater than Player 2
's score.
Function Description
Complete the function playSegments
in the editor.
playSegments
has the following parameter:
int coins[n]
: denotes whether a video game segment contains a coin (1) or not (0)Returns
int
: the minimum number of segments Player 1 must play so that their score is greater than Player 2's scoreExample 1:
Input: coins = [1, 1, 0, 1]
Output: 2
Explanation:Player 1 has the following options:Play 0 segments. This would give them a score of 0. Player 2 would have a score of 3 - 1 = 2 (because they gain a point for each of the 3 segments with a coin, and lose 1 point for the segment without a coin) Play 1 segments. This would give them a score of 1. Player 2 would have a score of 2 - 1 = 1. Play 2 segments. This would give them a socre of 2. Player 2 would have a score of 1 - 1 = 0. Only in this last case, by player 2 segments, would player 1's score be greater than player 2's. Therefore, return the answer 2.
1 <= n <= 105
coins[i] is either 1 or 0
input:
output: