MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / maxWidthRamp

Method maxWidthRamp

MaximumWidthRamp.java:2–22  ·  view source on GitHub ↗
(int[] nums)

Source from the content-addressed store, hash-verified

1class Solution {
2 public int maxWidthRamp(int[] nums) {
3 Stack<Integer> stack = new Stack<>(); //N
4 int n = nums.length;
5 // N
6 for(int i=0;i<n;i++){
7 if(stack.isEmpty() || nums[stack.peek()] > nums[i]){
8 stack.push(i);
9 }
10 }
11 int ans = Integer.MIN_VALUE;
12 // N + N-1
13 for(int i=n-1;i>=0;i--){
14 while(!stack.isEmpty() && nums[stack.peek()] <= nums[i]){
15 ans = Math.max(ans, i - stack.pop());
16 }
17 if(stack.isEmpty()){
18 break;
19 }
20 }
21 return ans;
22 }
23}

Callers

nothing calls this directly

Calls 3

pushMethod · 0.80
popMethod · 0.80
isEmptyMethod · 0.45

Tested by

no test coverage detected