File tree Expand file tree Collapse file tree 4 files changed +98
-0
lines changed
solution/0700-0799/0769.Max Chunks To Make Sorted Expand file tree Collapse file tree 4 files changed +98
-0
lines changed Original file line number Diff line number Diff line change @@ -290,6 +290,42 @@ func maxChunksToSorted(arr []int) int {
290290}
291291```
292292
293+ #### TypeScript
294+
295+ ``` ts
296+ function maxChunksToSorted(arr : number []): number {
297+ const stk: number [] = [];
298+
299+ for (const x of arr ) {
300+ if (stk .at (- 1 )! > x ) {
301+ const top = stk .pop ()! ;
302+ while (stk .length && stk .at (- 1 )! > x ) stk .pop ();
303+ stk .push (top );
304+ } else stk .push (x );
305+ }
306+
307+ return stk .length ;
308+ }
309+ ```
310+
311+ #### JavaScript
312+
313+ ``` js
314+ function maxChunksToSorted (arr ) {
315+ const stk = [];
316+
317+ for (const x of arr) {
318+ if (stk .at (- 1 ) > x) {
319+ const top = stk .pop ();
320+ while (stk .length && stk .at (- 1 ) > x) stk .pop ();
321+ stk .push (top);
322+ } else stk .push (x);
323+ }
324+
325+ return stk .length ;
326+ }
327+ ```
328+
293329<!-- tabs: end -->
294330
295331<!-- solution: end -->
Original file line number Diff line number Diff line change @@ -275,6 +275,42 @@ func maxChunksToSorted(arr []int) int {
275275}
276276```
277277
278+ #### TypeScript
279+
280+ ``` ts
281+ function maxChunksToSorted(arr : number []): number {
282+ const stk: number [] = [];
283+
284+ for (const x of arr ) {
285+ if (stk .at (- 1 )! > x ) {
286+ const top = stk .pop ()! ;
287+ while (stk .length && stk .at (- 1 )! > x ) stk .pop ();
288+ stk .push (top );
289+ } else stk .push (x );
290+ }
291+
292+ return stk .length ;
293+ }
294+ ```
295+
296+ #### JavaScript
297+
298+ ``` js
299+ function maxChunksToSorted (arr ) {
300+ const stk = [];
301+
302+ for (const x of arr) {
303+ if (stk .at (- 1 ) > x) {
304+ const top = stk .pop ();
305+ while (stk .length && stk .at (- 1 ) > x) stk .pop ();
306+ stk .push (top);
307+ } else stk .push (x);
308+ }
309+
310+ return stk .length ;
311+ }
312+ ```
313+
278314<!-- tabs: end -->
279315
280316<!-- solution: end -->
Original file line number Diff line number Diff line change 1+ function maxChunksToSorted ( arr ) {
2+ const stk = [ ] ;
3+
4+ for ( const x of arr ) {
5+ if ( stk . at ( - 1 ) > x ) {
6+ const top = stk . pop ( ) ;
7+ while ( stk . length && stk . at ( - 1 ) > x ) stk . pop ( ) ;
8+ stk . push ( top ) ;
9+ } else stk . push ( x ) ;
10+ }
11+
12+ return stk . length ;
13+ }
Original file line number Diff line number Diff line change 1+ function maxChunksToSorted ( arr : number [ ] ) : number {
2+ const stk : number [ ] = [ ] ;
3+
4+ for ( const x of arr ) {
5+ if ( stk . at ( - 1 ) ! > x ) {
6+ const top = stk . pop ( ) ! ;
7+ while ( stk . length && stk . at ( - 1 ) ! > x ) stk . pop ( ) ;
8+ stk . push ( top ) ;
9+ } else stk . push ( x ) ;
10+ }
11+
12+ return stk . length ;
13+ }
You can’t perform that action at this time.
0 commit comments