-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13.java
More file actions
46 lines (36 loc) · 1.24 KB
/
13.java
File metadata and controls
46 lines (36 loc) · 1.24 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
// Q13. Sum of Subarray Minimums
import java.util.*;
public class Solution {
public int sumSubarrayMins(int[] arr) {
int n = arr.length;
int MOD = 1_000_000_007;
long res = 0;
int[] left = new int[n];
int[] right = new int[n];
Deque<Integer> stack = new ArrayDeque<>();
for (int i = 0; i < n; i++) {
while (!stack.isEmpty() && arr[stack.peek()] > arr[i]) {
stack.pop();
}
left[i] = stack.isEmpty() ? -1 : stack.peek();
stack.push(i);
}
stack.clear();
for (int i = n - 1; i >= 0; i--) {
while (!stack.isEmpty() && arr[stack.peek()] >= arr[i]) {
stack.pop();
}
right[i] = stack.isEmpty() ? n : stack.peek();
stack.push(i);
}
for (int i = 0; i < n; i++) {
res = (res + (long) arr[i] * (i - left[i]) * (right[i] - i)) % MOD;
}
return (int) res;
}
public static void main(String[] args) {
Solution s = new Solution();
int[] arr = {3, 1, 2, 4};
System.out.println("Sum of subarray minimums: " + s.sumSubarrayMins(arr));
}
}