Skip to main content

Maximum Total Sum with Threshold Constraints - Video Solutions

MediumArrayGreedySortingHeap (Priority Queue)

Maximum Total Sum With Threshold Constraints

Owen Wu
4:3643 views
1 video solution available

Maximum Total Sum with Threshold Constraints - Video Solution

Watch the video solution for Maximum Total Sum with Threshold Constraints, a medium level problem involving Array, Greedy, Sorting. This walkthrough by Owen Wu has 43 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

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 i such that threshold[i] <= step.
    • If no such index exists, the process ends.
  • Add nums[i] to your running total.
  • Mark index i as used and increment step by 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, choose i = 1 since threshold[1] <= step. The total sum becomes 10. Mark index 1.
  • At step = 2, choose i = 4 since threshold[4] <= step. The total sum becomes 11. Mark index 4.
  • At step = 3, choose i = 5 since threshold[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, choose i = 3 since threshold[3] <= step. The total sum becomes 13. Mark index 3.
  • At step = 2, choose i = 2 since threshold[2] <= step. The total sum becomes 23. Mark index 2.
  • At step = 3, choose i = 1 since threshold[1] <= step. The total sum becomes 29. Mark index 1.
  • At step = 4, choose i = 0 since threshold[0] <= step. The total sum becomes 31. Mark index 0.
  • After step = 4 all indices have been chosen, so the process ends.

 

Constraints:

  • n == nums.length == threshold.length
  • 1 <= n <= 105
  • 1 <= nums[i] <= 109
  • 1 <= threshold[i] <= n
Read full problem with examples

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.

Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force AssignmentO(n!)O(n)Conceptual baseline to understand constraints; not practical for large inputs
Greedy + SortingO(n log n)O(1) or O(log n)Best general solution when all thresholds are known upfront
Greedy with Max HeapO(n log n)O(n)Useful when thresholds are processed dynamically or streaming