From 227972358277d4be0b02e1959dc3ab8a4c389b49 Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Thu, 10 Oct 2024 22:20:20 +0530 Subject: [PATCH] Create 962. Maximum Width Ramp --- 962. Maximum Width Ramp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 962. Maximum Width Ramp diff --git a/962. Maximum Width Ramp b/962. Maximum Width Ramp new file mode 100644 index 0000000..922a10a --- /dev/null +++ b/962. Maximum Width Ramp @@ -0,0 +1,22 @@ +class Solution { +public: + int maxWidthRamp(vector& nums) { + //decreasing stack + + int n = nums.size(); + stack st; + for(int i = 0; i < n; i++){ + if(st.empty() || nums[st.top()] > nums[i]){ + st.push(i); + } + } + int ans = 0; + for(int i = n-1; i > 0; i--){ + while(!st.empty() && nums[st.top()] <= nums[i]){ + ans = max(ans, i - st.top()); + st.pop(); + } + } + return ans; + } +};