Maximum Total Sum with Threshold Constraints - Solution & Explanation
Problem Statement
You are given two integer arrays nums and threshold, both of length n.
Starting at step = 1, you perform the following repeatedly:
- Choose an unused index
isuch thatthreshold[i] <= step.- If no such index exists, the process ends.
- Add
nums[i]to your running total. - Mark index
ias used and incrementstepby 1.
Return the maximum total sum you can obtain by choosing indices optimally.
Example 1:
Input: nums = [1,10,4,2,1,6], threshold = [5,1,5,5,2,2]
Output: 17
Explanation:
- At
step = 1, choosei = 1sincethreshold[1] <= step. The total sum becomes 10. Mark index 1. - At
step = 2, choosei = 4sincethreshold[4] <= step. The total sum becomes 11. Mark index 4. - At
step = 3, choosei = 5sincethreshold[5] <= step. The total sum becomes 17. Mark index 5. - At
step = 4, we cannot choose indices 0, 2, or 3 because their thresholds are> 4, so we end the process.
Example 2:
Input: nums = [4,1,5,2,3], threshold = [3,3,2,3,3]
Output: 0
Explanation:
At step = 1 there is no index i with threshold[i] <= 1, so the process ends immediately. Thus, the total sum is 0.
Example 3:
Input: nums = [2,6,10,13], threshold = [2,1,1,1]
Output: 31
Explanation:
- At
step = 1, choosei = 3sincethreshold[3] <= step. The total sum becomes 13. Mark index 3. - At
step = 2, choosei = 2sincethreshold[2] <= step. The total sum becomes 23. Mark index 2. - At
step = 3, choosei = 1sincethreshold[1] <= step. The total sum becomes 29. Mark index 1. - At
step = 4, choosei = 0sincethreshold[0] <= step. The total sum becomes 31. Mark index 0. - After
step = 4all indices have been chosen, so the process ends.
Constraints:
n == nums.length == threshold.length1 <= n <= 1051 <= nums[i] <= 1091 <= threshold[i] <= n
Approach Overview
Problem Overview: You are given an array where each element represents the maximum value (threshold) you can assign to a position. Choose a value for every position such that it does not exceed its threshold and all chosen values remain valid under the constraint rules. The objective is to maximize the total sum of the chosen values.
Approach 1: Greedy + Sorting (O(n log n) time, O(1) extra space)
The optimal strategy is greedy. Larger thresholds should receive larger assigned values because they give more flexibility. Start by sorting the threshold array in descending order using sorting. Track the largest value you can still assign, initially equal to the largest threshold. For each element, assign min(current_threshold, previous_assigned - 1). This enforces the decreasing constraint while staying within each threshold. If the computed value becomes zero or negative, a valid assignment is impossible. Otherwise, accumulate the value into the total sum. This works because greedily giving the largest feasible value early preserves room for smaller thresholds later.
Approach 2: Greedy with Max Heap (Priority Queue) (O(n log n) time, O(n) space)
A heap (priority queue) can also simulate the greedy selection process. Push all thresholds into a max heap and repeatedly take the largest available threshold. Maintain the previously assigned value and reduce it by one each step to enforce the constraint. If the popped threshold is smaller than the allowed value, clamp the assignment to that threshold. The heap ensures you always process the largest remaining candidate first. This approach is conceptually similar to sorting but useful when values arrive dynamically or when you want explicit control over priority ordering.
Why the Greedy Strategy Works
The key insight is that assigning the largest possible value to the largest threshold cannot harm future decisions. Smaller thresholds already limit future choices, so delaying large assignments would only reduce the achievable total. Greedy ordering after sorting ensures every step locally maximizes the sum while keeping the global constraints satisfied.
Recommended for interviews: The Greedy + Sorting approach is what interviewers typically expect. It demonstrates understanding of greedy decision making and efficient use of arrays with sorting. Mentioning a brute-force search shows baseline reasoning, but deriving the sorted greedy rule signals strong problem-solving ability.
Solution
We observe that at each step, we want to select the largest number among those that satisfy the condition to add to the total sum. Therefore, we can use a greedy approach to solve this problem.
We first sort an index array idx of length n in ascending order by their corresponding thresholds. Then, we use a sorted set or priority queue (max heap) to maintain the numbers that currently satisfy the condition. At each step, we add all numbers whose thresholds are less than or equal to the current step number into the sorted set or priority queue, and then select the largest number among them to add to the total sum. If the sorted set or priority queue is empty at this point, it means there are no numbers that satisfy the condition, and we end the process.
The time complexity is O(n times log n), and the space complexity is O(n), where n is the length of the array nums.
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Assignment | O(n!) | O(n) | Conceptual baseline to understand constraints; not practical for large inputs |
| Greedy + Sorting | O(n log n) | O(1) or O(log n) | Best general solution when all thresholds are known upfront |
| Greedy with Max Heap | O(n log n) | O(n) | Useful when thresholds are processed dynamically or streaming |
Video Solution
Maximum Total Sum With Threshold Constraints • Owen Wu • 43 views views
Frequently Asked Questions
Is Maximum Total Sum with Threshold Constraints easy or hard?
Maximum Total Sum with Threshold Constraints Python/Java solution
How to solve Maximum Total Sum with Threshold Constraints in O(n)?
What is the best approach for Maximum Total Sum with Threshold Constraints?
Is Maximum Total Sum with Threshold Constraints asked at Google/Amazon/Meta?
What data structure is used in Maximum Total Sum with Threshold Constraints?
What is the time complexity of Maximum Total Sum with Threshold Constraints?
Ready to solve this problem?
Practice Maximum Total Sum with Threshold Constraints with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor