Maximum Subarray Sum After Multiplier Problem
Description
LeetCode Problem 3976.
You are given an integer array nums and a positive integer k.
You must choose exactly one subarray of nums and perform exactly one of the following operations:
- Multiply each number in the chosen subarray by
k. - Divide each number in the chosen subarray by
k.- When dividing a positive number by
k, use the floor value of the division result. - When dividing a negative number by
k, use the ceiling value of the division result.
- When dividing a positive number by
Return the maximum possible sum of a non-empty subarray in the resulting array.
Note that the subarray chosen for the operation and the subarray chosen for the sum may be different.
Example 1:
Input: nums = [1,-2,3,4,-5], k = 2
Output: 14
Explanation:
- Multiply each number in the subarray [3, 4] by 2.
- This results in nums = [1, -2, 6, 8, -5].
- The subarray with the largest sum is [6, 8], so the output is 6 + 8 = 14.
Example 2:
Input: nums = [-5,-4,-3], k = 2
Output: -1
Explanation:
- Divide each number in the subarray [-3] by 2.
- This results in nums = [-5, -4, -1].
- The subarray with the largest sum is [-1], so the output is -1.
Constraints:
1 <= nums.length <= 10^5-10^5 <= nums[i] <= 10^51 <= k <= 10^5
Solution
Approach: Dynamic Programming with Four States
The array has one subarray that gets operated on (multiplied or divided) and one subarray that gets summed. They can overlap in any way, so we track, for the best sum of a subarray ending at the current element, how the operated part sits relative to it:
- State 0: no element of the sum has been operated on yet.
- State 1: the current element lies in the multiplied part.
- State 2: the current element lies in the divided part.
- State 3: the operated part has already ended (it started inside the sum, and the current element is after it).
For element x, with the best values of the previous element in f:
f0 = max(f0, 0) + xf1 = max(f0, f1, 0) + x * kf2 = max(f0, f2, 0) + trunc(x / k)f3 = max(f1, f2, f3) + x
The 0 inside the maxima means the sum may start at the current element. The division rounds toward zero: the floor for positive numbers and the ceiling for negative ones. The answer is the largest value of any state at any position. Taking the sum from a state that never operated on anything is fine because the operation can be applied to some other part of the array without changing that sum (and when it cannot, a multiplication of a positive element is never worse).
Complexity: O(n) time and O(1) extra space, where n is the length of nums. The reference solutions keep the whole table, which is O(n) space.
from math import inf
class Solution:
def maxSubarraySum(self, nums: List[int], k: int) -> int:
n = len(nums)
f = [[-inf] * 4 for _ in range(n + 1)]
f[0][0] = 0
ans = -inf
for i, x in enumerate(nums, 1):
f[i][0] = max(f[i - 1][0], 0) + x
f[i][1] = max(f[i - 1][0], f[i - 1][1], 0) + x * k
f[i][2] = max(f[i - 1][0], f[i - 1][2], 0) + (x // k if x >= 0 else -(-x // k)) # floor for positives, ceiling for negatives
f[i][3] = max(f[i - 1][1], f[i - 1][2], f[i - 1][3]) + x
ans = max(ans, max(f[i]))
return ans