Skip to content

Commit 2279723

Browse files
authored
Create 962. Maximum Width Ramp
1 parent 4f2d700 commit 2279723

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

962. Maximum Width Ramp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
int maxWidthRamp(vector<int>& nums) {
4+
//decreasing stack
5+
6+
int n = nums.size();
7+
stack<int> st;
8+
for(int i = 0; i < n; i++){
9+
if(st.empty() || nums[st.top()] > nums[i]){
10+
st.push(i);
11+
}
12+
}
13+
int ans = 0;
14+
for(int i = n-1; i > 0; i--){
15+
while(!st.empty() && nums[st.top()] <= nums[i]){
16+
ans = max(ans, i - st.top());
17+
st.pop();
18+
}
19+
}
20+
return ans;
21+
}
22+
};

0 commit comments

Comments
 (0)