-
Notifications
You must be signed in to change notification settings - Fork 0
/
SumSubarrayMinimum.java
61 lines (56 loc) · 2.12 KB
/
SumSubarrayMinimum.java
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
# Problem Statement: find the sum of the minimums of all the subarrays of a given array
---------------------------------------------------------------
# Approach :
- calculate the next smaller element and the previous smaller or equal elemet for each element in the given array
- then calculate the left and right index of that smaller element and use math formula to calculate the number of subarray existing
- then multiply the number of the index and the element to get the total and add it to the previous to it.
---------------------------------------------------------------
*/
import java.util.ArrayList;
import java.util.Stack;
class Solution {
public int sumSubarrayMins(int[] arr) {
int n = arr.length;
ArrayList<Integer> nse = findNSE(arr, n);
ArrayList<Integer> psee = findPSEE(arr, n);
long total = 0;
int mod = (int)(1e9 + 7);
for (int i = 0; i < n; i++) {
long left = i - psee.get(i);
long right = nse.get(i) - i;
total = (total + (right * left % mod * arr[i] % mod) % mod) % mod;
}
return (int) total;
}
public ArrayList<Integer> findNSE(int[] arr, int n) {
ArrayList<Integer> nse = new ArrayList<>(n);
Stack<Integer> st = new Stack<>();
for (int i = 0; i < n; i++) {
nse.add(n); // Initialize nse with n
}
for (int i = n - 1; i >= 0; i--) {
while (!st.isEmpty() && arr[st.peek()] >= arr[i]) {
st.pop();
}
nse.set(i, st.isEmpty() ? n : st.peek());
st.push(i);
}
return nse;
}
public ArrayList<Integer> findPSEE(int[] arr, int n) {
ArrayList<Integer> psee = new ArrayList<>(n);
Stack<Integer> st = new Stack<>();
for (int i = 0; i < n; i++) {
psee.add(-1); // Initialize psee with -1
}
for (int i = 0; i < n; i++) {
while (!st.isEmpty() && arr[st.peek()] > arr[i]) {
st.pop();
}
psee.set(i, st.isEmpty() ? -1 : st.peek());
st.push(i);
}
return psee;
}
}