The Maximum Number of Operations With the Same Score II coding problem is a more complex version of "I". Given an array of integers nums, you can perform an operation by selecting two elements, nums[i] and nums[j], removing them, and recording their sum. You want to find the maximum number of operations you can perform such that all recorded sums are equal. Unlike "I", the elements don't have to be adjacent, and the target sum is not fixed by the first two elements but can be any achievable sum.
Apple and Microsoft ask this problem to test dynamic programming with memoization, especially in scenarios where the "target" value is not immediately obvious. The key insight is that since all sums must be equal, we can try three possible initial sums (first two, first and last, last two) and then use DP to check the maximum operations for each, finding the overall maximum.
Dynamic Programming with Memoization:
Since all operations must result in the same score, there are only three possible values for this common score: nums[0] + nums[1], nums[0] + nums[n-1], or nums[n-2] + nums[n-1].
We can define a recursive function solve(left, right, target) which returns the maximum operations possible on the subarray nums[left...right] given a fixed target sum.
Base cases:
left >= right, return 0 (no elements or one element, no more operations).Recursive step:
res = 0nums[left] and nums[left+1]: if nums[left] + nums[left+1] == target, then res = max(res, 1 + solve(left+2, right, target)).nums[left] and nums[right]: if nums[left] + nums[right] == target, then res = max(res, 1 + solve(left+1, right-1, target)).nums[right-1] and nums[right]: if nums[right-1] + nums[right] == target, then res = max(res, 1 + solve(left, right-2, target)).Memoize the results of solve(left, right, target) to avoid recomputing. The target can be passed explicitly or inferred. Since there are only three possible targets, we can run the DP three times.
The state for DP would be dp[left][right]. The final answer would be max(solve(0, n-1, nums[0]+nums[1]), solve(0, n-1, nums[0]+nums[n-1]), solve(0, n-1, nums[n-2]+nums[n-1])).
nums = [3, 2, 1, 2, 3, 4] n = 6.
Possible target sums:
nums[0] + nums[1] = 3 + 2 = 5nums[0] + nums[5] = 3 + 4 = 7nums[4] + nums[5] = 3 + 4 = 7Let's try target = 5:
solve(0, 5, 5):
(3,2) -> 1 + solve(2,5,5)
solve(2,5,5) on [1,2,3,4]
(1,2)? Sum=3 != 5.(1,4)? Sum=5. -> 1 + solve(3,4,5)
solve(3,4,5) on [2,3]
(2,3)? Sum=5. -> 1 + solve(5,4,5) -> 1+0 = 1. So for [2,3] gives 1 op.
So for [1,2,3,4] it gives 1+1=2 ops.(3,4)? Sum=7 != 5.
So, for target 5, we can get 1 + 2 = 3 operations. (e.g., (3,2), (1,4), (2,3)).Let's try target = 7:
solve(0, 5, 7):
(3,4) (nums[0], nums[5]) -> 1 + solve(1,4,7)
solve(1,4,7) on [2,1,2,3]
(2,3) (nums[1], nums[4]) -> 1 + solve(2,3,7)
solve(2,3,7) on [1,2]
(1,2)? Sum=3 != 7.
So, for [1,2] it gives 0 ops.
So, for [2,1,2,3] it gives 1+0=1 ops.
So, for target 7, we can get 1+1=2 operations.Max is 3.
target would be another DP state, making it too complex.left and right indices are updated correctly after each choice (e.g., left+2, right for removing left, left+1).For the Array Memoization Dynamic Programming interview pattern, always look for problems where the "optimal substructure" (overlapping subproblems) and "optimal choice" (greedy choice reduces problem to subproblem) are present. When the target value is implicit, consider what fixed values it must be.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Remove Boxes | Hard | Solve | |
| Selling Pieces of Wood | Hard | Solve | |
| Length of Longest V-Shaped Diagonal Segment | Hard | Solve | |
| Maximum Energy Boost From Two Drinks | Medium | Solve | |
| Partition Array for Maximum Sum | Medium | Solve |