-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy path1856-maximum-subarray-min-product.js
More file actions
47 lines (43 loc) · 1.61 KB
/
1856-maximum-subarray-min-product.js
File metadata and controls
47 lines (43 loc) · 1.61 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* 1856. Maximum Subarray Min-Product
* https://leetcode.com/problems/maximum-subarray-min-product/
* Difficulty: Medium
*
* The min-product of an array is equal to the minimum value in the array multiplied by
* the array's sum.
* - For example, the array [3,2,5] (minimum value is 2) has a min-product of
* 2 * (3+2+5) = 2 * 10 = 20.
*
* Given an array of integers nums, return the maximum min-product of any non-empty
* subarray of nums. Since the answer may be large, return it modulo 109 + 7.
*
* Note that the min-product should be maximized before performing the modulo operation.
* Testcases are generated such that the maximum min-product without modulo will fit in
* a 64-bit signed integer.
*
* A subarray is a contiguous part of an array.
*/
/**
* @param {number[]} nums
* @return {number}
*/
var maxSumMinProduct = function(nums) {
const modulo = 1000000007n;
const stack = [];
const prefixSums = [0n];
let maxMinProduct = 0n;
for (let i = 0; i < nums.length; i++) {
prefixSums.push(prefixSums[i] + BigInt(nums[i]));
}
for (let right = 0; right <= nums.length; right++) {
const current = right < nums.length ? BigInt(nums[right]) : 0n;
while (stack.length && stack[stack.length - 1].value > current) {
const { index, value } = stack.pop();
const left = stack.length ? stack[stack.length - 1].index + 1 : 0;
const subarraySum = prefixSums[right] - prefixSums[left];
maxMinProduct = maxMinProduct > value * subarraySum ? maxMinProduct : value * subarraySum;
}
stack.push({ index: right, value: current });
}
return Number(maxMinProduct % modulo);
};