Skip to content

Commit 1e058a8

Browse files
authored
Create 2289-steps-to-make-array-non-decreasing.js
1 parent 92d492f commit 1e058a8

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var totalSteps = function(nums) {
6+
const n = nums.length
7+
let res = 0, j = -1;
8+
const dp = Array(n).fill(0), stack = Array(n).fill(0);
9+
for (let i = n - 1; i >= 0; --i) {
10+
while (j >= 0 && nums[i] > nums[stack[j]]) {
11+
dp[i] = Math.max(++dp[i], dp[stack[j--]])
12+
res = Math.max(res, dp[i])
13+
}
14+
stack[++j] = i
15+
}
16+
return res
17+
};

0 commit comments

Comments
 (0)