Maximum Total Sum with Threshold Constraints
You are given two integer arrays nums and threshold, each of length n.
You start with:
- step = 1
- total = 0
- No index is used initially.
At each step:
- You may choose any unused index i such that threshold[i] <= step.
- If no such index exists, the process stops.
- Add nums[i] to total.
- Mark index i as used.
- Increment step by 1.
Return the maximum total sum that can be obtained by choosing indices in an optimal order.
Example 1:
Input: nums = [8, 3, 5, 7], threshold = [1, 3, 2, 2]
Output: 23
Explanation:
- Step 1 → Only index 0 is valid (threshold[0] = 1), Total = 8
- Step 2 → Indices 2 and 3 are now valid. Choose index 3 (value = 7), Total = 15
- Step 3 → Index 2 is valid. Choose index 2 (value = 5), Total = 20
- Step 4 → Index 1 becomes valid. Choose index 1 (value = 3), Total = 23
Example 2:
Input: nums = [6, 9, 4], threshold = [2, 3, 3]
Output: 0
Explanation:
- At Step 1, no index satisfies threshold[i] <= 1, The process stops immediately.
- Total sum = 0.
Now Your Turn!
Pick the correct output for the given inputInput: nums = [5, 2, 9], threshold = [1, 2, 2]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- n == nums.length == threshold.length
- 1 ≤ n ≤ 105
- 1 ≤ nums[i] ≤ 109
- 1 ≤ threshold[i] ≤ n