Problem · Array
Starlight
Learn this problemProblem statement
The beauty of an integer array is the maximum sum of a contiguous subarray. Choosing no elements is allowed, so beauty is never negative.
You may choose one contiguous segment and multiply every value in that segment by z. This amplification may be used at most once.
Return the maximum beauty obtainable after the optional amplification.
Function
enhanceLuminescence(arr: int[], z: int) → longExamples
Example 1
arr = [3, 2]z = 2return = 10Amplifying the entire array produces [6, 4], whose sum is 10.
Example 2
arr = [-5, 9, -2, 1, -6]z = -3return = 30Amplify [-2, 1, -6] to obtain [6, -3, 18]. The best subarray is then [9, 6, -3, 18], with sum 30.
Example 3
arr = [1, 2, 4, 5]z = -2return = 12Skipping amplification keeps the full-array sum 12, which is optimal.
Constraints
1 <= arr.length <= 2 * 10^5.-10^9 <= arr[i] <= 10^9.-100 <= z <= 100.