Skip to content

Commit 89f859e

Browse files
committed
Add solution #2289
1 parent 683fba6 commit 89f859e

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2079,6 +2079,7 @@
20792079
2286|[Booking Concert Tickets in Groups](./solutions/2286-booking-concert-tickets-in-groups.js)|Hard|
20802080
2287|[Rearrange Characters to Make Target String](./solutions/2287-rearrange-characters-to-make-target-string.js)|Easy|
20812081
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|
20822083
2291|[Maximum Profit From Trading Stocks](./solutions/2291-maximum-profit-from-trading-stocks.js)|Medium|
20832084
2293|[Min Max Game](./solutions/2293-min-max-game.js)|Easy|
20842085
2294|[Partition Array Such That Maximum Difference Is K](./solutions/2294-partition-array-such-that-maximum-difference-is-k.js)|Medium|
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
};

0 commit comments

Comments
 (0)