Maximum Total Reward Using Operations I - Problem
You are given an integer array rewardValues of length n, representing the values of rewards. Initially, your total reward x is 0, and all indices are unmarked.
You are allowed to perform the following operation any number of times:
- Choose an unmarked index
ifrom the range[0, n - 1]. - If
rewardValues[i]is greater than your current total rewardx, then addrewardValues[i]tox(i.e.,x = x + rewardValues[i]), and mark the indexi.
Return an integer denoting the maximum total reward you can collect by performing the operations optimally.
Input & Output
Example 1 — Basic Case
$
Input:
rewardValues = [1,1,3,3]
›
Output:
4
💡 Note:
Sort to [1,1,3,3]. Start with total=0. Select first 1 (1>0), total=1. Skip second 1 (1≤1). Select first 3 (3>1), total=4. Skip second 3 (3≤4). Maximum reward is 4.
Example 2 — All Selectable
$
Input:
rewardValues = [1,6,4,3,2]
›
Output:
11
💡 Note:
Sort to [1,2,3,4,6]. Start with total=0. Select 1 (total=1), select 2 (total=3), select 3 (total=6), skip 4 (4≤6), skip 6 (6≤6). Maximum reward is 6.
Example 3 — Single Element
$
Input:
rewardValues = [5]
›
Output:
5
💡 Note:
Only one element [5]. Since 5 > 0, we can select it. Maximum reward is 5.
Constraints
- 1 ≤ rewardValues.length ≤ 2000
- 1 ≤ rewardValues[i] ≤ 2000
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array of reward values [1,1,3,3]
2
Process
Sort and greedily select rewards > current total
3
Output
Maximum total reward achievable: 4
Key Takeaway
🎯 Key Insight: Sort rewards and greedily select each reward that's greater than current total to maximize the final sum
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code