-
Notifications
You must be signed in to change notification settings - Fork 41
/
solution.ts
33 lines (28 loc) · 968 Bytes
/
solution.ts
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
function maxSumMinProduct(nums: number[]): number {
let result = 0n;
const stack:number[] = [];
const sums = new Array<number>(nums.length+1);
sums[0] = 0;
for(let i=0;i<nums.length;i++){
sums[i+1] = sums[i]+nums[i];
while(stack.length && nums[stack[stack.length-1]]>=nums[i]){
const index = stack.pop()!;
const left = stack.length>0?stack[stack.length-1]: -1;
const sum = sums[i]-sums[left+1];
const product = BigInt(sum)*BigInt(nums[index]);
if(product>result){
result = product;
}
}
stack.push(i)
}
for(let i=stack.length-1;i>-1;i--){
const left = i>0? stack[i-1]: -1;
const sum = sums[nums.length]-sums[left+1];
const product = BigInt(sum)*BigInt(nums[stack[i]]);
if(product>result){
result = product
}
}
return Number( result%(BigInt(10**9+7)));
};