Skip to main content

Maximum Total Value of Covered Indices - Solution & Explanation

MediumArrayStringDynamic ProgrammingGreedy4 min readAsked at: Amazon
Practice this problem

Problem Statement

You are given an integer array nums of length n and a binary string s of length n, where s[i] == '1' means index i initially contains a token and s[i] == '0' means it does not.

You may perform the following operation any number of times:

  • Choose a token currently located at index i, where i > 0, such that this token has not been moved before.
  • Move this token from index i to index i - 1.

An index is considered covered if it contains a token after all moves.

Return an integer denoting the maximum total value of nums at the covered indices after optimally performing the operations.

 

Example 1:

Input: nums = [9,2,6,1], s = "0101"

Output: 15

Explanation:

  • Initially, indices 1 and 3 contain tokens.
  • Move the token from index 3 to index 2.
  • Move the token from index 1 to index 0.
  • The covered indices are [0, 2], so the total value is nums[0] + nums[2] = 9 + 6 = 15.

Example 2:

Input: nums = [5,1,4], s = "001"

Output: 4

Explanation:

  • Initially, only index 2 contains a token.
  • It is optimal to leave the token at index 2.
  • The covered index is [2], so the total value is nums[2] = 4.

Example 3:

Input: nums = [9,3,5], s = "011"

Output: 14

Explanation:

  • Initially, indices 1 and 2 contain tokens.
  • Move the token from index 1 to index 0.
  • The covered indices are [0, 2], so the total value is nums[0] + nums[2] = 9 + 5 = 14.

 

Constraints:

  • 1 <= n == nums.length == s.length <= 105
  • 1 <= nums[i] <= 105
  • ​​​​​​​s[i] is either '0' or '1'

Approach Overview

Problem Overview: You are given an array where each index has a value and a list of intervals. Choosing an interval covers all indices within its range. The goal is to pick intervals so the total value of the covered indices is maximized, typically under the constraint that intervals cannot overlap.

Approach 1: Brute Force Subset Enumeration (Exponential Time)

The most direct idea is to try every subset of intervals. For each subset, check if the intervals overlap and compute the total value of the indices they cover. You can precompute prefix sums of the array so that the value of an interval [l, r] is calculated in O(1). This approach requires evaluating up to 2^m subsets where m is the number of intervals, leading to O(2^m * m) time and O(1) extra space. It quickly becomes impractical but helps illustrate the decision process behind selecting compatible intervals.

Approach 2: Dynamic Programming with Interval Sorting (O(m^2))

First compute prefix sums of the value array so the value of each interval can be obtained in constant time. Sort intervals by their ending index. Let dp[i] represent the maximum value achievable considering the first i intervals. For each interval i, you either skip it or take it and add its value to the best non-overlapping interval before it. Finding the previous compatible interval requires scanning backward, which results in O(m^2) time and O(m) space. This approach clearly demonstrates the structure of the optimal subproblem.

Approach 3: Weighted Interval Scheduling with Binary Search (O(m log m))

The optimal solution improves the DP transition. After sorting intervals by end index, use binary search to find the last interval whose end is strictly before the current interval's start. Prefix sums still provide the value of each interval in O(1). The transition becomes dp[i] = max(dp[i-1], value(i) + dp[prev]), where prev is found with binary search. Sorting takes O(m log m) and each DP step performs one binary search, resulting in O(m log m) time and O(m) space. This pattern is the classic weighted interval scheduling technique often seen in dynamic programming and interval problems, combined with binary search for fast compatibility checks.

Recommended for interviews: Interviewers expect the weighted interval scheduling solution with binary search. Starting with the brute-force idea shows you understand the search space, but transitioning to DP with sorted intervals and binary search demonstrates strong problem decomposition and optimization skills.

Solutions for this problem are being prepared.

Try solving it yourself

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Subset EnumerationO(2^m * m)O(1)Only for very small number of intervals or conceptual understanding
Dynamic Programming with Backward ScanO(m^2)O(m)When constraints are moderate and simplicity is preferred
Weighted Interval Scheduling + Binary SearchO(m log m)O(m)Best general solution for large inputs with many intervals

Video Solution

Leetcode 3952 | Maximum Total Value of Covered Indices | Leetcode biweekly contest 184 • CodeWithMeGuys • 677 views views

Watch 7 more video solutions →

Frequently Asked Questions

Is Maximum Total Value of Covered Indices easy or hard?
Maximum Total Value of Covered Indices is considered a medium difficulty problem. The challenge lies in recognizing the weighted interval scheduling pattern and optimizing the DP transition with binary search to reach O(m log m) time.
Maximum Total Value of Covered Indices Python/Java solution
Typical implementations compute prefix sums, sort intervals by end index, and use DP with binary search to find the last compatible interval. The same logic works across Python, Java, and C++ because it relies on standard arrays, sorting, and binary search utilities.
How to solve Maximum Total Value of Covered Indices in O(m log m)?
First compute prefix sums so the value of any interval [l, r] can be calculated instantly. Sort intervals by their ending position. For each interval, use binary search to locate the last interval that ends before its start, then apply the DP relation dp[i] = max(dp[i-1], value(i) + dp[prev]).
What is the best approach for Maximum Total Value of Covered Indices?
The best approach is weighted interval scheduling combined with prefix sums and binary search. Prefix sums compute the value of any interval in O(1), intervals are sorted by end index, and binary search finds the previous non-overlapping interval. The dynamic programming transition then builds the optimal answer in O(m log m) time.
Is Maximum Total Value of Covered Indices asked at Google/Amazon/Meta?
Problems based on weighted interval scheduling frequently appear in interviews at companies like Google, Amazon, and Meta. The exact problem title may vary, but the underlying pattern of selecting non-overlapping intervals with maximum total value is a common interview theme.
What data structure is used in Maximum Total Value of Covered Indices?
The solution primarily uses arrays for dynamic programming, prefix sums for constant-time range value calculation, and binary search on a sorted interval list. Together these structures enable efficient compatibility checks between intervals.
What is the time complexity of Maximum Total Value of Covered Indices?
The optimal solution runs in O(m log m) time where m is the number of intervals. Sorting intervals takes O(m log m), and each DP step performs a binary search to find the last compatible interval. Space complexity is O(m) for the DP array.

Ready to solve this problem?

Practice Maximum Total Value of Covered Indices with our built-in code editor and test cases.

Practice on FleetCode