-
Notifications
You must be signed in to change notification settings - Fork 42
/
solution.ts
36 lines (34 loc) · 1.01 KB
/
solution.ts
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
function maxWidthRamp (A: number[]): number {
const stack = [0, ];
for (let i = 1; i < A.length; i++) {
if (A[i] < A[stack[stack.length - 1]]) {
stack.push(i);
}
}
let result = 0;
for (let i = A.length - 1; i > 0; i--) {
while (stack.length && stack[stack.length - 1] >= i) {
stack.pop();
}
while (stack.length && A[stack[stack.length - 1]] <= A[i]) {
result = Math.max(result, i - stack.pop()!);
}
}
return result;
}
function maxWidthRamp2(nums: number[]): number {
const stack:number[] = [];
let result = 0;
for(let i=0;i<nums.length;i++){
if(stack.length === 0 || nums[i]<nums[stack[stack.length-1]] ){
stack.push(i);
}
}
for(let i=nums.length-1;i>-1 && stack.length>0;i--){
while(stack.length && nums[stack[stack.length-1]]<=nums[i] ){
const index = stack.pop()!;
result = Math.max(result,i-index);
}
}
return result
};