Skip to main content

Minimum Levels to Gain More Points - Video Solutions

MediumArrayPrefix Sum

3096. Minimum Levels to Gain More Points | Prefix Sums | Arrays

Aryan Mittal
7:421,168 views
7 video solutions available

Minimum Levels to Gain More Points - Video Solution

Watch 7 video solutions for Minimum Levels to Gain More Points, a medium level problem involving Array, Prefix Sum. This walkthrough by Aryan Mittal has 1,168 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

Problem Statement

You are given a binary array possible of length n.

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 ith 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.

At the start of the game, Alice will play some levels in the given order starting from the 0th level, after which Bob will play for the rest of the levels.

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.

Return the minimum number of levels Alice should play to gain more points. If this is not possible, return -1.

Note that each player must play at least 1 level.

 

Example 1:

Input: possible = [1,0,1,0]

Output: 1

Explanation:

Let's look at all the levels that Alice can play up to:

  • 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.
  • 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.
  • 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.

Alice must play a minimum of 1 level to gain more points.

Example 2:

Input: possible = [1,1,1,1,1]

Output: 3

Explanation:

Let's look at all the levels that Alice can play up to:

  • If Alice plays only level 0 and Bob plays the rest of the levels, Alice has 1 point, while Bob has 4 points.
  • If Alice plays till level 1 and Bob plays the rest of the levels, Alice has 2 points, while Bob has 3 points.
  • If Alice plays till level 2 and Bob plays the rest of the levels, Alice has 3 points, while Bob has 2 points.
  • If Alice plays till level 3 and Bob plays the rest of the levels, Alice has 4 points, while Bob has 1 point.

Alice must play a minimum of 3 levels to gain more points.

Example 3:

Input: possible = [0,0]

Output: -1

Explanation:

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.

 

Constraints:

  • 2 <= n == possible.length <= 105
  • possible[i] is either 0 or 1.
Read full problem with examples

Approach Overview

Problem Overview: You are given an array where each level either gives +1 point or -1 point depending on whether it is winnable. Alice plays the first k levels and Bob plays the remaining levels. The task is to find the minimum k such that Aliceโ€™s total score is strictly greater than Bobโ€™s, while ensuring Bob still plays at least one level.

Approach 1: Prefix Sum and Greedy Selection (O(n) time, O(1) space)

Convert the game outcome into a score representation: treat 1 as +1 point and 0 as -1. First compute the total score of all levels. Then iterate from left to right while maintaining a running prefix score for Alice. Bobโ€™s score at any split point can be computed as totalScore - prefixScore. The earliest index where prefixScore > totalScore - prefixScore satisfies the requirement. Return that position as the minimum number of levels Alice should play, ensuring the split happens before the last element so Bob has at least one level. This works because each iteration updates Aliceโ€™s score incrementally and compares it with Bobโ€™s remaining score. The approach only requires a single pass over the array with a running prefix sum, making it the most efficient solution.

Approach 2: Binary Search on Points Difference (O(n log n) time, O(n) space)

Precompute a prefix sum array where each entry stores the cumulative score up to that level. With prefix sums available, Aliceโ€™s score for the first k levels is prefix[k] and Bobโ€™s score becomes prefix[n] - prefix[k]. Instead of scanning linearly, perform a binary search on the split point k. For each candidate midpoint, calculate Aliceโ€™s and Bobโ€™s scores using constant-time prefix queries. If Aliceโ€™s score is already greater, move the search left to find a smaller valid split; otherwise move right. This approach is slightly heavier because it builds a prefix array and performs multiple checks, but it demonstrates how prefix sums enable fast range queries that pair well with binary search.

Recommended for interviews: The Prefix Sum + Greedy scan is the expected solution. It runs in linear time, uses constant extra space, and clearly demonstrates understanding of prefix sums and score comparisons. Showing the brute reasoningโ€”compute both playersโ€™ scores and check the earliest valid splitโ€”proves correctness, while the optimized single-pass implementation highlights strong algorithmic thinking.

Complexity Analysis

ApproachTimeSpaceWhen to Use
Prefix Sum and Greedy SelectionO(n)O(1)Best general solution. Single pass with running score comparison.
Binary Search on Points DifferenceO(n log n)O(n)When prefix sums are already precomputed or when demonstrating binary search over split positions.