FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
LeetCode-Solutions/C++/sum-of-subarray-minimums.cpp at master · tehsints/LeetCode-Solutions · GitHub
tehsints
/
LeetCode-Solutions
Public
forked from
kamyu104/LeetCode-Solutions
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
LeetCode-Solutions
/
C++
/
sum-of-subarray-minimums.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
40 lines (36 loc) · 1.04 KB
Breadcrumbs
LeetCode-Solutions
/
C++
/
sum-of-subarray-minimums.cpp
Copy path
File metadata and controls
40 lines (36 loc) · 1.04 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
//
Time: O(n)
//
Space: O(n)
//
Ascending stack solution
class
Solution
{
public:
int
sumSubarrayMins
(vector<
int
>& A) {
static
const
int
M =
1e9
+
7
;
vector<
int
>
left
(A.
size
());
stack<pair<
int
,
int
>> s1;
for
(
int
i =
0
; i < A.
size
(); ++i) {
int
count =
1
;
while
(!s1.
empty
() && s1.
top
().
first
> A[i]) {
count += s1.
top
().
second
;
s1.
pop
();
}
s1.
emplace
(A[i], count);
left[i] = count;
}
vector<
int
>
right
(A.
size
());
stack<pair<
int
,
int
>> s2;
for
(
int
i = A.
size
() -
1
; i >=
0
; --i) {
int
count =
1
;
while
(!s2.
empty
() && s2.
top
().
first
>= A[i]) {
count += s2.
top
().
second
;
s2.
pop
();
}
s2.
emplace
(A[i], count);
right[i] = count;
}
int
result =
0
;
for
(
int
i =
0
; i < A.
size
(); ++i) {
result = (result + A[i] * left[i] * right[i]) % M;
}
return
result;
}
};
Back
|
FazBrowse Home
|
New Git URL