File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change 2079
2079
2286|[ Booking Concert Tickets in Groups] ( ./solutions/2286-booking-concert-tickets-in-groups.js ) |Hard|
2080
2080
2287|[ Rearrange Characters to Make Target String] ( ./solutions/2287-rearrange-characters-to-make-target-string.js ) |Easy|
2081
2081
2288|[ Apply Discount to Prices] ( ./solutions/2288-apply-discount-to-prices.js ) |Medium|
2082
+ 2289|[ Steps to Make Array Non-decreasing] ( ./solutions/2289-steps-to-make-array-non-decreasing.js ) |Medium|
2082
2083
2291|[ Maximum Profit From Trading Stocks] ( ./solutions/2291-maximum-profit-from-trading-stocks.js ) |Medium|
2083
2084
2293|[ Min Max Game] ( ./solutions/2293-min-max-game.js ) |Easy|
2084
2085
2294|[ Partition Array Such That Maximum Difference Is K] ( ./solutions/2294-partition-array-such-that-maximum-difference-is-k.js ) |Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 2289. Steps to Make Array Non-decreasing
3
+ * https://leetcode.com/problems/steps-to-make-array-non-decreasing/
4
+ * Difficulty: Medium
5
+ *
6
+ * You are given a 0-indexed integer array nums. In one step, remove all elements nums[i]
7
+ * where nums[i - 1] > nums[i] for all 0 < i < nums.length.
8
+ *
9
+ * Return the number of steps performed until nums becomes a non-decreasing array.
10
+ */
11
+
12
+ /**
13
+ * @param {number[] } nums
14
+ * @return {number }
15
+ */
16
+ var totalSteps = function ( nums ) {
17
+ const n = nums . length ;
18
+ const stack = [ ] ;
19
+ const steps = new Array ( n ) . fill ( 0 ) ;
20
+ let result = 0 ;
21
+
22
+ for ( let i = n - 1 ; i >= 0 ; i -- ) {
23
+ while ( stack . length && nums [ i ] > nums [ stack [ stack . length - 1 ] ] ) {
24
+ const j = stack . pop ( ) ;
25
+ steps [ i ] = Math . max ( steps [ i ] + 1 , steps [ j ] ) ;
26
+ }
27
+ result = Math . max ( result , steps [ i ] ) ;
28
+ stack . push ( i ) ;
29
+ }
30
+
31
+ return result ;
32
+ } ;
You can’t perform that action at this time.
0 commit comments