File tree 2 files changed +36
-1
lines changed
2 files changed +36
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,046 LeetCode solutions in JavaScript
1
+ # 1,047 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcode.com/ ] ( https://leetcode.com/ )
4
4
770
770
958|[ Check Completeness of a Binary Tree] ( ./solutions/0958-check-completeness-of-a-binary-tree.js ) |Medium|
771
771
959|[ Regions Cut By Slashes] ( ./solutions/0959-regions-cut-by-slashes.js ) |Medium|
772
772
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|
773
774
966|[ Vowel Spellchecker] ( ./solutions/0966-vowel-spellchecker.js ) |Medium|
774
775
970|[ Powerful Integers] ( ./solutions/0970-powerful-integers.js ) |Easy|
775
776
976|[ Largest Perimeter Triangle] ( ./solutions/0976-largest-perimeter-triangle.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments