File tree 7 files changed +134
-24
lines changed
0768.Max Chunks To Make Sorted II
0769.Max Chunks To Make Sorted
7 files changed +134
-24
lines changed Original file line number Diff line number Diff line change @@ -142,22 +142,44 @@ func maxChunksToSorted(arr []int) int {
142
142
143
143
``` ts
144
144
function maxChunksToSorted(arr : number []): number {
145
- let stack = []; // 左进左出
146
- for (let num of arr ) {
147
- if (stack .length && num < stack [0 ]) {
148
- let max = stack .shift ();
149
- while (stack .length && num < stack [0 ]) {
150
- stack .shift ();
145
+ const stack = [];
146
+ for (const num of arr ) {
147
+ if (stack .length !== 0 && num < stack [stack . length - 1 ]) {
148
+ const max = stack .pop ();
149
+ while (stack .length !== 0 && num < stack [stack . length - 1 ]) {
150
+ stack .pop ();
151
151
}
152
- stack .unshift (max );
152
+ stack .push (max );
153
153
} else {
154
- stack .unshift (num );
154
+ stack .push (num );
155
155
}
156
156
}
157
157
return stack .length ;
158
158
}
159
159
```
160
160
161
+ ### ** Rust**
162
+
163
+ ``` rust
164
+ impl Solution {
165
+ pub fn max_chunks_to_sorted (arr : Vec <i32 >) -> i32 {
166
+ let mut stack = vec! [];
167
+ for num in arr . iter () {
168
+ if ! stack . is_empty () && num < stack . last (). unwrap () {
169
+ let max = stack . pop (). unwrap ();
170
+ while ! stack . is_empty () && num < stack . last (). unwrap () {
171
+ stack . pop ();
172
+ }
173
+ stack . push (max )
174
+ } else {
175
+ stack . push (* num );
176
+ }
177
+ }
178
+ stack . len () as i32
179
+ }
180
+ }
181
+ ```
182
+
161
183
### ** ...**
162
184
163
185
```
Original file line number Diff line number Diff line change @@ -130,22 +130,44 @@ func maxChunksToSorted(arr []int) int {
130
130
131
131
``` ts
132
132
function maxChunksToSorted(arr : number []): number {
133
- let stack = [];
134
- for (let num of arr ) {
135
- if (stack .length && num < stack [0 ]) {
136
- let max = stack .shift ();
137
- while (stack .length && num < stack [0 ]) {
138
- stack .shift ();
133
+ const stack = [];
134
+ for (const num of arr ) {
135
+ if (stack .length !== 0 && num < stack [stack . length - 1 ]) {
136
+ const max = stack .pop ();
137
+ while (stack .length !== 0 && num < stack [stack . length - 1 ]) {
138
+ stack .pop ();
139
139
}
140
- stack .unshift (max );
140
+ stack .push (max );
141
141
} else {
142
- stack .unshift (num );
142
+ stack .push (num );
143
143
}
144
144
}
145
145
return stack .length ;
146
146
}
147
147
```
148
148
149
+ ### ** Rust**
150
+
151
+ ``` rust
152
+ impl Solution {
153
+ pub fn max_chunks_to_sorted (arr : Vec <i32 >) -> i32 {
154
+ let mut stack = vec! [];
155
+ for num in arr . iter () {
156
+ if ! stack . is_empty () && num < stack . last (). unwrap () {
157
+ let max = stack . pop (). unwrap ();
158
+ while ! stack . is_empty () && num < stack . last (). unwrap () {
159
+ stack . pop ();
160
+ }
161
+ stack . push (max )
162
+ } else {
163
+ stack . push (* num );
164
+ }
165
+ }
166
+ stack . len () as i32
167
+ }
168
+ }
169
+ ```
170
+
149
171
### ** ...**
150
172
151
173
```
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn max_chunks_to_sorted ( arr : Vec < i32 > ) -> i32 {
3
+ let mut stack = vec ! [ ] ;
4
+ for num in arr. iter ( ) {
5
+ if !stack. is_empty ( ) && num < stack. last ( ) . unwrap ( ) {
6
+ let max = stack. pop ( ) . unwrap ( ) ;
7
+ while !stack. is_empty ( ) && num < stack. last ( ) . unwrap ( ) {
8
+ stack. pop ( ) ;
9
+ }
10
+ stack. push ( max)
11
+ } else {
12
+ stack. push ( * num) ;
13
+ }
14
+ }
15
+ stack. len ( ) as i32
16
+ }
17
+ }
Original file line number Diff line number Diff line change 1
1
function maxChunksToSorted ( arr : number [ ] ) : number {
2
- let stack = [ ] ; // 左进左出
3
- for ( let num of arr ) {
4
- if ( stack . length && num < stack [ 0 ] ) {
5
- let max = stack . shift ( ) ;
6
- while ( stack . length && num < stack [ 0 ] ) {
7
- stack . shift ( ) ;
2
+ const stack = [ ] ;
3
+ for ( const num of arr ) {
4
+ if ( stack . length !== 0 && num < stack [ stack . length - 1 ] ) {
5
+ const max = stack . pop ( ) ;
6
+ while ( stack . length !== 0 && num < stack [ stack . length - 1 ] ) {
7
+ stack . pop ( ) ;
8
8
}
9
- stack . unshift ( max ) ;
9
+ stack . push ( max ) ;
10
10
} else {
11
- stack . unshift ( num ) ;
11
+ stack . push ( num ) ;
12
12
}
13
13
}
14
14
return stack . length ;
Original file line number Diff line number Diff line change @@ -149,6 +149,24 @@ function maxChunksToSorted(arr: number[]): number {
149
149
}
150
150
```
151
151
152
+ ### ** Rust**
153
+
154
+ ``` rust
155
+ impl Solution {
156
+ pub fn max_chunks_to_sorted (arr : Vec <i32 >) -> i32 {
157
+ let mut res = 0 ;
158
+ let mut max = 0 ;
159
+ for i in 0 .. arr . len () {
160
+ max = max . max (arr [i ]);
161
+ if max == i as i32 {
162
+ res += 1 ;
163
+ }
164
+ }
165
+ res
166
+ }
167
+ }
168
+ ```
169
+
152
170
### ** ...**
153
171
154
172
```
Original file line number Diff line number Diff line change @@ -133,6 +133,24 @@ function maxChunksToSorted(arr: number[]): number {
133
133
}
134
134
```
135
135
136
+ ### ** Rust**
137
+
138
+ ``` rust
139
+ impl Solution {
140
+ pub fn max_chunks_to_sorted (arr : Vec <i32 >) -> i32 {
141
+ let mut res = 0 ;
142
+ let mut max = 0 ;
143
+ for i in 0 .. arr . len () {
144
+ max = max . max (arr [i ]);
145
+ if max == i as i32 {
146
+ res += 1 ;
147
+ }
148
+ }
149
+ res
150
+ }
151
+ }
152
+ ```
153
+
136
154
### ** ...**
137
155
138
156
```
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn max_chunks_to_sorted ( arr : Vec < i32 > ) -> i32 {
3
+ let mut res = 0 ;
4
+ let mut max = 0 ;
5
+ for i in 0 ..arr. len ( ) {
6
+ max = max. max ( arr[ i] ) ;
7
+ if max == i as i32 {
8
+ res += 1 ;
9
+ }
10
+ }
11
+ res
12
+ }
13
+ }
You can’t perform that action at this time.
0 commit comments