3976. Maximum Subarray Sum After Multiplier
Description
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 is6 + 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 <= 105-105 <= nums[i] <= 1051 <= k <= 105
Solutions
Solution 1: Dynamic Programming
We define \(f[i][j]\) as the maximum subarray sum ending at \(nums[i]\) with current state \(j\). There are \(4\) states for \(j\):
- State \(0\): the current subarray has not undergone any operation yet;
- State \(1\): the current subarray is being multiplied by \(k\);
- State \(2\): the current subarray is being divided by \(k\);
- State \(3\): the operation on the current subarray has been completed.
Initially, \(f[0][0] = 0\), and all other \(f[i][j] = -\infty\).
Next, we consider the state transitions. For the \(i\)-th number \(nums[i]\), we can choose not to perform any operation, multiply by \(k\), divide by \(k\), or continue after the operation has been completed:
- If we perform no operation, then \(f[i][0] = \max(f[i-1][0], 0) + nums[i]\);
- If we multiply by \(k\), then \(f[i][1] = \max(f[i-1][0], f[i-1][1], 0) + nums[i] \times k\);
- If we divide by \(k\), then \(f[i][2] = \max(f[i-1][0], f[i-1][2], 0) + \lfloor \frac{nums[i]}{k} \rfloor\);
- If the operation has been completed, then \(f[i][3] = \max(f[i-1][1], f[i-1][2], f[i-1][3]) + nums[i]\).
We take the maximum among all states as the answer.
The time complexity is \(O(n)\), and the space complexity is \(O(n)\). Here, \(n\) is the length of the array \(\textit{nums}\).
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |