-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumofSubarrayMinimums.java
More file actions
135 lines (118 loc) · 3.62 KB
/
SumofSubarrayMinimums.java
File metadata and controls
135 lines (118 loc) · 3.62 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package local;
//907. Sum of Subarray Minimums
import java.util.Arrays;
import java.util.Stack;
public class SumofSubarrayMinimums {
public static void main(String[] args) {
// int []arr={1,4,6,7,3,7,8,1};
int[]arr={3,1,2,4};
Solution obj=new Solution();
int ans=obj.sumSubarrayMins(arr);
System.out.println(ans);
}
}
//Having Only little bit problem
/*
class Solution {
public int sumSubarrayMins(int[] arr)
{
//Finding the next smallest element
//for that we traverse from back
int n=arr.length;
int[]NextSmallest=new int[n];
Stack<Integer> stack=new Stack<>();
NextSmallest[n-1]=n;
stack.push(n-1);
for(int i=n-2 ;i>=0;i--)
{
while(!stack.isEmpty() && arr[stack.peek()]>arr[i])
{
stack.pop();
}
if(stack.isEmpty())
{
NextSmallest[i]=n;
}else{
NextSmallest[i]=stack.peek();
}
stack.push(i);
}
System.out.println(Arrays.toString(NextSmallest));
//Finding the previous Smallest Element
//for that we traverse from starting
int[] PreviousSmallest=new int[n];
stack.clear();
PreviousSmallest[0]=-1;
stack.push(0);
for(int i=1;i<n;i++)
{
while (!stack.isEmpty() && arr[stack.peek()]>arr[i]) {
stack.pop();
}
if(stack.isEmpty())
{
PreviousSmallest[i]=-1;
}else{
PreviousSmallest[i]=stack.peek();
}
stack.push(i);
}
System.out.println(Arrays.toString(PreviousSmallest));
//Writing Main Code
int result=0;
int mod=(int)(1e9+7);
for (int i = 0; i < arr.length; i++) {
int right=NextSmallest[i]-i;
int left=i-PreviousSmallest[i];
result=(result+(int)(left*right*arr[i]))%mod;
}
return result;
}
}
*/
class Solution extends HelperMethod{ // inherit the class
public int sumSubarrayMins(int[] arr) {
HelperMethod help = new HelperMethod(); // object of helper fun to access the method.
int[] nextSE = help.nextSmaller(arr);
int[] prevSE = help.previousSmaller(arr);
long total = 0;
int mod = (int)(1e9+7);
for (int i = 0; i < arr.length; i++) {
int left = i - prevSE[i];
int right = nextSE[i] - i;
total = (total + (long)left * right * arr[i]) % mod;
}
return (int) total;
}
}
//purpose of creating another class just coz of organized the code..
class HelperMethod{
// Find the next smaller element
public int[] nextSmaller(int[] arr) {
int n = arr.length;
int[] nse = new int[n];
Stack<Integer> stk = new Stack<>();
for (int i = n - 1; i >= 0; i--) {
while (!stk.isEmpty() && arr[stk.peek()] > arr[i]) {
stk.pop();
}
nse[i] = (stk.isEmpty()) ? n : stk.peek();
stk.push(i);
}
return nse;
}
// Find the previous smaller element
public int[] previousSmaller(int[] arr) {
int n = arr.length;
int[] pse = new int[n];
Stack<Integer> stk = new Stack<>();
for (int i = 0; i < n; i++) {
while (!stk.isEmpty() && arr[stk.peek()] >= arr[i]) {
stk.pop();
}
pse[i] = (stk.isEmpty()) ? -1 : stk.peek();
stk.push(i);
}
return pse;
}
}