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:
i from the range [0, n - 1].rewardValues[i] is greater than your current total reward x, then add rewardValues[i] to x (i.e., x = x + rewardValues[i]), and mark the index i.Return an integer denoting the maximum total reward you can collect by performing the operations optimally.
Example 1:
Input: rewardValues = [1,1,3,3]
Output: 4
Explanation:
During the operations, we can choose to mark the indices 0 and 2 in order, and the total reward will be 4, which is the maximum.
Example 2:
Input: rewardValues = [1,6,4,3,2]
Output: 11
Explanation:
Mark the indices 0, 2, and 1 in order. The total reward will then be 11, which is the maximum.
Constraints:
1 <= rewardValues.length <= 5 * 1041 <= rewardValues[i] <= 5 * 104When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The problem asks for the maximum total reward attainable by sequentially picking reward values strictly greater than the current sum. Because each chosen reward must be strictly greater than the accumulated total so far, sorting unique reward values allows us to model reachable totals using bitset operations. We track achievable sums as bits in an integer and transition reachable states efficiently using bitwise shifts and masks.
Here's how the algorithm would work step-by-step:
def max_total_reward(reward_values: list[int]) -> int:
# Remove duplicate values and sort the rewards in ascending order.
sorted_rewards = sorted(set(reward_values))
reachable_mask = 1
# Iterate through each unique reward value to update achievable total rewards.
for current_reward in sorted_rewards:
valid_mask = reachable_mask & ((1 << current_reward) - 1)
reachable_mask |= valid_mask << current_reward
# The maximum achievable reward corresponds to the most significant bit position.
return reachable_mask.bit_length() - 1
To maximize our total reward without getting bogged down by endless combinations, we first organize all available reward values from smallest to largest. We then use a clever digital trick to keep track of every possible reward total we can achieve as we consider each reward one by one.
Here's how the algorithm would work step-by-step:
def max_total_reward(reward_values):
unique_sorted_rewards = sorted(list(set(reward_values)))
# Using a bitmask allows us to evaluate all possible sums simultaneously in O(1) time per shift.
reachable_totals_mask = 1
for current_reward in unique_sorted_rewards:
# We only shift the mask up to the current reward value to strictly enforce the problem rules.
mask_filter = (1 << current_reward) - 1
reachable_totals_mask |= (reachable_totals_mask & mask_filter) << current_reward
# The highest bit set in the final bitmask represents the maximum attainable reward total.
return reachable_totals_mask.bit_length() - 1
| Case | How to Handle |
|---|---|
| Input array has a single element | The algorithm directly returns that single element as the maximum total reward since no further operations are possible. |
| Inputs contain duplicate reward values | Sorting and removing duplicates via a set ensures we only process distinct reward values as specified in the problem statement. |
| Maximum possible reward exceeds standard integer limits | Using bitset operations or boolean arrays prevents integer overflow and handles large state spaces efficiently within memory limits. |
| Array values are extremely large up to the problem constraints | The DP state space scales up to twice the maximum element, so sorting and pruning unreachable states prevents Memory Limit Exceeded errors. |
| Reward values are small and tightly clustered | Bitset optimization processes multiple states simultaneously in parallel word-sized chunks to ensure optimal time complexity. |
| No operations can be performed after the first pick | The initial reward starts at zero and the algorithm naturally falls back to the maximum single reward if no larger values exist to continue the sequence. |