Skip to content

Commit 1c8f4e0

Browse files
committed
Add solution #962
1 parent 0a288b5 commit 1c8f4e0

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,046 LeetCode solutions in JavaScript
1+
# 1,047 LeetCode solutions in JavaScript
22

33
[https://leetcode.com/](https://leetcode.com/)
44

@@ -770,6 +770,7 @@
770770
958|[Check Completeness of a Binary Tree](./solutions/0958-check-completeness-of-a-binary-tree.js)|Medium|
771771
959|[Regions Cut By Slashes](./solutions/0959-regions-cut-by-slashes.js)|Medium|
772772
960|[Delete Columns to Make Sorted III](./solutions/0960-delete-columns-to-make-sorted-iii.js)|Hard|
773+
962|[Maximum Width Ramp](./solutions/0962-maximum-width-ramp.js)|Medium|
773774
966|[Vowel Spellchecker](./solutions/0966-vowel-spellchecker.js)|Medium|
774775
970|[Powerful Integers](./solutions/0970-powerful-integers.js)|Easy|
775776
976|[Largest Perimeter Triangle](./solutions/0976-largest-perimeter-triangle.js)|Easy|

solutions/0962-maximum-width-ramp.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 962. Maximum Width Ramp
3+
* https://leetcode.com/problems/maximum-width-ramp/
4+
* Difficulty: Medium
5+
*
6+
* A ramp in an integer array nums is a pair (i, j) for which i < j and nums[i] <= nums[j].
7+
* The width of such a ramp is j - i.
8+
*
9+
* Given an integer array nums, return the maximum width of a ramp in nums. If there is no
10+
* ramp in nums, return 0.
11+
*/
12+
13+
/**
14+
* @param {number[]} nums
15+
* @return {number}
16+
*/
17+
var maxWidthRamp = function(nums) {
18+
const stack = [];
19+
let result = 0;
20+
21+
for (let i = 0; i < nums.length; i++) {
22+
if (!stack.length || nums[stack.at(-1)] > nums[i]) {
23+
stack.push(i);
24+
}
25+
}
26+
27+
for (let j = nums.length - 1; j >= 0; j--) {
28+
while (stack.length && nums[stack.at(-1)] <= nums[j]) {
29+
result = Math.max(result, j - stack.pop());
30+
}
31+
}
32+
33+
return result;
34+
};

0 commit comments

Comments
 (0)