-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
/
Copy pathSolution.ts
38 lines (38 loc) · 1.04 KB
/
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
34
35
36
37
38
function maxSumMinProduct(nums: number[]): number {
const n = nums.length;
const left: number[] = Array(n).fill(-1);
const right: number[] = Array(n).fill(n);
const stk: number[] = [];
for (let i = 0; i < n; ++i) {
while (stk.length && nums[stk.at(-1)!] >= nums[i]) {
stk.pop();
}
if (stk.length) {
left[i] = stk.at(-1)!;
}
stk.push(i);
}
stk.length = 0;
for (let i = n - 1; i >= 0; --i) {
while (stk.length && nums[stk.at(-1)!] > nums[i]) {
stk.pop();
}
if (stk.length) {
right[i] = stk.at(-1)!;
}
stk.push(i);
}
const s: number[] = Array(n + 1).fill(0);
for (let i = 0; i < n; ++i) {
s[i + 1] = s[i] + nums[i];
}
let ans: bigint = 0n;
const mod = 10 ** 9 + 7;
for (let i = 0; i < n; ++i) {
const t = BigInt(nums[i]) * BigInt(s[right[i]] - s[left[i] + 1]);
if (ans < t) {
ans = t;
}
}
return Number(ans % BigInt(mod));
}