|
| 1 | +3096\. Minimum Levels to Gain More Points |
| 2 | + |
| 3 | +Medium |
| 4 | + |
| 5 | +You are given a binary array `possible` of length `n`. |
| 6 | + |
| 7 | +Alice and Bob are playing a game that consists of `n` levels. Some of the levels in the game are **impossible** to clear while others can **always** be cleared. In particular, if `possible[i] == 0`, then the <code>i<sup>th</sup></code> level is **impossible** to clear for **both** the players. A player gains `1` point on clearing a level and loses `1` point if the player fails to clear it. |
| 8 | + |
| 9 | +At the start of the game, Alice will play some levels in the **given order** starting from the <code>0<sup>th</sup></code> level, after which Bob will play for the rest of the levels. |
| 10 | + |
| 11 | +Alice wants to know the **minimum** number of levels she should play to gain more points than Bob, if both players play optimally to **maximize** their points. |
| 12 | + |
| 13 | +Return _the **minimum** number of levels Alice should play to gain more points_. _If this is **not** possible, return_ `-1`. |
| 14 | + |
| 15 | +**Note** that each player must play at least `1` level. |
| 16 | + |
| 17 | +**Example 1:** |
| 18 | + |
| 19 | +**Input:** possible = [1,0,1,0] |
| 20 | + |
| 21 | +**Output:** 1 |
| 22 | + |
| 23 | +**Explanation:** |
| 24 | + |
| 25 | +Let's look at all the levels that Alice can play up to: |
| 26 | + |
| 27 | +* If Alice plays only level 0 and Bob plays the rest of the levels, Alice has 1 point, while Bob has -1 + 1 - 1 = -1 point. |
| 28 | +* If Alice plays till level 1 and Bob plays the rest of the levels, Alice has 1 - 1 = 0 points, while Bob has 1 - 1 = 0 points. |
| 29 | +* If Alice plays till level 2 and Bob plays the rest of the levels, Alice has 1 - 1 + 1 = 1 point, while Bob has -1 point. |
| 30 | + |
| 31 | +Alice must play a minimum of 1 level to gain more points. |
| 32 | + |
| 33 | +**Example 2:** |
| 34 | + |
| 35 | +**Input:** possible = [1,1,1,1,1] |
| 36 | + |
| 37 | +**Output:** 3 |
| 38 | + |
| 39 | +**Explanation:** |
| 40 | + |
| 41 | +Let's look at all the levels that Alice can play up to: |
| 42 | + |
| 43 | +* If Alice plays only level 0 and Bob plays the rest of the levels, Alice has 1 point, while Bob has 4 points. |
| 44 | +* If Alice plays till level 1 and Bob plays the rest of the levels, Alice has 2 points, while Bob has 3 points. |
| 45 | +* If Alice plays till level 2 and Bob plays the rest of the levels, Alice has 3 points, while Bob has 2 points. |
| 46 | +* If Alice plays till level 3 and Bob plays the rest of the levels, Alice has 4 points, while Bob has 1 point. |
| 47 | + |
| 48 | +Alice must play a minimum of 3 levels to gain more points. |
| 49 | + |
| 50 | +**Example 3:** |
| 51 | + |
| 52 | +**Input:** possible = [0,0] |
| 53 | + |
| 54 | +**Output:** \-1 |
| 55 | + |
| 56 | +**Explanation:** |
| 57 | + |
| 58 | +The only possible way is for both players to play 1 level each. Alice plays level 0 and loses 1 point. Bob plays level 1 and loses 1 point. As both players have equal points, Alice can't gain more points than Bob. |
| 59 | + |
| 60 | +**Constraints:** |
| 61 | + |
| 62 | +* <code>2 <= n == possible.length <= 10<sup>5</sup></code> |
| 63 | +* `possible[i]` is either `0` or `1`. |
0 commit comments