Skip to content

Commit 03733ab

Browse files
authored
Update 2289-steps-to-make-array-non-decreasing.js
1 parent 29d9d8c commit 03733ab

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

2289-steps-to-make-array-non-decreasing.js

+26
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,29 @@ const totalSteps = function(nums) {
4040

4141
return res
4242
};
43+
44+
// another
45+
46+
/**
47+
* @param {number[]} nums
48+
* @return {number}
49+
*/
50+
const totalSteps = function(nums) {
51+
let res = 0
52+
const stk = []
53+
for(const e of nums) {
54+
let steps = 1
55+
while(stk.length && e >= stk[stk.length - 1][0]) {
56+
const tmp = stk.pop()
57+
steps = Math.max(tmp[1] + 1, steps)
58+
}
59+
if(stk.length === 0) steps = 0
60+
else {
61+
res = Math.max(res, steps)
62+
}
63+
stk.push([e, steps])
64+
}
65+
return res
66+
};
67+
68+

0 commit comments

Comments
 (0)