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:
We can select rewards in optimal order. One optimal sequence: select 2 (total=2), then 3 (total=5), then 6 (total=11). Other values become unselectable. Maximum reward is 11.
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code