File tree Expand file tree Collapse file tree 3 files changed +45
-2
lines changed
solution/2200-2299/2289.Steps to Make Array Non-decreasing Expand file tree Collapse file tree 3 files changed +45
-2
lines changed Original file line number Diff line number Diff line change @@ -142,7 +142,21 @@ func max(a, b int) int {
142
142
### ** TypeScript**
143
143
144
144
``` ts
145
-
145
+ function totalSteps(nums : number []): number {
146
+ let ans = 0 ;
147
+ let stack = [];
148
+ for (let num of nums ) {
149
+ let max = 0 ;
150
+ while (stack .length && stack [0 ][0 ] <= num ) {
151
+ max = Math .max (stack [0 ][1 ], max );
152
+ stack .shift ();
153
+ }
154
+ if (stack .length ) max ++ ;
155
+ ans = Math .max (max , ans );
156
+ stack .unshift ([num , max ]);
157
+ }
158
+ return ans ;
159
+ };
146
160
```
147
161
148
162
### ** ...**
Original file line number Diff line number Diff line change @@ -130,7 +130,21 @@ func max(a, b int) int {
130
130
### ** TypeScript**
131
131
132
132
``` ts
133
-
133
+ function totalSteps(nums : number []): number {
134
+ let ans = 0 ;
135
+ let stack = [];
136
+ for (let num of nums ) {
137
+ let max = 0 ;
138
+ while (stack .length && stack [0 ][0 ] <= num ) {
139
+ max = Math .max (stack [0 ][1 ], max );
140
+ stack .shift ();
141
+ }
142
+ if (stack .length ) max ++ ;
143
+ ans = Math .max (max , ans );
144
+ stack .unshift ([num , max ]);
145
+ }
146
+ return ans ;
147
+ };
134
148
```
135
149
136
150
### ** ...**
Original file line number Diff line number Diff line change
1
+ function totalSteps ( nums : number [ ] ) : number {
2
+ let ans = 0 ;
3
+ let stack = [ ] ;
4
+ for ( let num of nums ) {
5
+ let max = 0 ;
6
+ while ( stack . length && stack [ 0 ] [ 0 ] <= num ) {
7
+ max = Math . max ( stack [ 0 ] [ 1 ] , max ) ;
8
+ stack . shift ( ) ;
9
+ }
10
+ if ( stack . length ) max ++ ;
11
+ ans = Math . max ( max , ans ) ;
12
+ stack . unshift ( [ num , max ] ) ;
13
+ }
14
+ return ans ;
15
+ } ;
You can’t perform that action at this time.
0 commit comments