Maximum Total Reward Using Operations I
01 · Problem
You are given an integer array rewardValues. Your total reward x starts at 0, and every index starts unmarked. You may repeat the following any number of times (including zero):
- Choose an unmarked index
isuch thatrewardValues[i]is strictly greater than the currentx. - Add
rewardValues[i]toxand mark indexi.
Each index can be used at most once, and you may pick indices in any order. Return the maximum total reward x you can end up with.
02 · Examples
rewardValues = [2,5,3,8]
15
Take 2 (x = 2), then 5 since 5 > 2 (x = 7), then 8 since 8 > 7 (x = 15). No ordering can reach more.
rewardValues = [4,4,4]
4
After taking one 4, x = 4 and the remaining values are not strictly greater, so the answer is 4.
rewardValues = [1,6,4,3,2]
11
Take 1 (x = 1), then 4 (x = 5), then 6 (x = 11). The final value can never reach 2 * 6 = 12.
03 · Constraints
- 011 <= rewardValues.length <= 2000
- 021 <= rewardValues[i] <= 2000
- 03Each index may be chosen at most once, and a value can be added only if it is strictly greater than the current total
04 · Optimal complexity
- Time
- O(n * M) where M = max(rewardValues)
- Space
- O(M)
Practice it alone or rehearse it as an interview.
Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.