|
| 1 | +--- |
| 2 | +id: max-total-reward |
| 3 | +title: Maximum Total Reward Using Operations |
| 4 | +level: hard |
| 5 | +sidebar_label: Max Total Reward |
| 6 | +tags: |
| 7 | +- Dynamic Programming |
| 8 | +- Bit Manipulation |
| 9 | +- C++ |
| 10 | +- Java |
| 11 | +- Python |
| 12 | +description: "This document provides solutions for finding the maximum total reward using dynamic programming and bit manipulation, implemented in C++, Java, and Python." |
| 13 | +--- |
| 14 | + |
| 15 | +## Problem |
| 16 | +Given a list of reward values, the goal is to determine the maximum total reward that can be achieved using a series of operations. |
| 17 | + |
| 18 | +## Solution |
| 19 | +The problem is solved using dynamic programming and bit manipulation. The approach involves maintaining a bitset or similar data structure to track achievable reward values and iterating through the sorted reward values to update this structure. |
| 20 | + |
| 21 | +## Code in Different Languages |
| 22 | + |
| 23 | +### C++ |
| 24 | +```cpp |
| 25 | +#include <vector> |
| 26 | +#include <bitset> |
| 27 | +#include <algorithm> |
| 28 | +#include <stdexcept> |
| 29 | +using namespace std; |
| 30 | + |
| 31 | +class Solution { |
| 32 | + public: |
| 33 | + int maxTotalReward(vector<int>& rewardValues) { |
| 34 | + constexpr int kPossibleRewards = 100000; |
| 35 | + bitset<kPossibleRewards> dp; |
| 36 | + dp[0] = true; |
| 37 | + |
| 38 | + sort(rewardValues.begin(), rewardValues.end()); |
| 39 | + |
| 40 | + for (const int num : rewardValues) { |
| 41 | + bitset<kPossibleRewards> newBits = dp; |
| 42 | + newBits <<= kPossibleRewards - num; |
| 43 | + newBits >>= kPossibleRewards - num; |
| 44 | + dp |= newBits << num; |
| 45 | + } |
| 46 | + |
| 47 | + for (int ans = kPossibleRewards - 1; ans >= 0; --ans) |
| 48 | + if (dp[ans]) |
| 49 | + return ans; |
| 50 | + |
| 51 | + throw runtime_error("Solution not found"); |
| 52 | + } |
| 53 | +}; |
| 54 | +``` |
| 55 | +
|
| 56 | +### Java |
| 57 | +```java |
| 58 | +import java.math.BigInteger; |
| 59 | +import java.util.Arrays; |
| 60 | +
|
| 61 | +class Solution { |
| 62 | + public int maxTotalReward(int[] rewardValues) { |
| 63 | + BigInteger one = BigInteger.ONE; |
| 64 | + BigInteger dp = one; // the possible rewards (initially, 0 is achievable) |
| 65 | +
|
| 66 | + Arrays.sort(rewardValues); |
| 67 | +
|
| 68 | + for (final int num : rewardValues) { |
| 69 | + BigInteger maskBitsLessThanNum = one.shiftLeft(num).subtract(one); |
| 70 | + BigInteger bitsLessThanNum = dp.and(maskBitsLessThanNum); |
| 71 | + dp = dp.or(bitsLessThanNum.shiftLeft(num)); |
| 72 | + } |
| 73 | +
|
| 74 | + return dp.bitLength() - 1; |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +### Python |
| 80 | +```python |
| 81 | +from typing import List |
| 82 | + |
| 83 | +class Solution: |
| 84 | + def maxTotalReward(self, rewardValues: List[int]) -> int: |
| 85 | + dp = 1 # the possible rewards (initially, 0 is achievable) |
| 86 | + |
| 87 | + for num in sorted(rewardValues): |
| 88 | + smallerNums = dp & ((1 << num) - 1) |
| 89 | + dp |= smallerNums << num |
| 90 | + |
| 91 | + return dp.bit_length() - 1 |
| 92 | + |
| 93 | +# Example usage |
| 94 | +sol = Solution() |
| 95 | +rewardValues = [1, 2, 3, 5] |
| 96 | +print(sol.maxTotalReward(rewardValues)) # Output: 10 |
| 97 | +``` |
| 98 | + |
| 99 | +# Complexity Analysis |
| 100 | +## Time Complexity: |
| 101 | +$O(n * k)$, where n is the number of reward values and k is the maximum possible reward value. Sorting the reward values takes O(n log n) time, and each bit manipulation operation is efficient. |
| 102 | + |
| 103 | +## Space Complexity: |
| 104 | +$O(k)$ |
| 105 | +Reason: The bitset or similar structure requires space proportional to the maximum possible reward value. |
0 commit comments