Skip to content

Commit 3a71796

Browse files
committed
feat: add rust solution to lc problems: No.0769,0768
- No.0769.Max Chunks To Make Sorted - No.0768.Max Chunks To Make Sorted II
1 parent c9b8e40 commit 3a71796

File tree

7 files changed

+134
-24
lines changed

7 files changed

+134
-24
lines changed

solution/0700-0799/0768.Max Chunks To Make Sorted II/README.md

+30-8
Original file line numberDiff line numberDiff line change
@@ -142,22 +142,44 @@ func maxChunksToSorted(arr []int) int {
142142

143143
```ts
144144
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();
151151
}
152-
stack.unshift(max);
152+
stack.push(max);
153153
} else {
154-
stack.unshift(num);
154+
stack.push(num);
155155
}
156156
}
157157
return stack.length;
158158
}
159159
```
160160

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+
161183
### **...**
162184

163185
```

solution/0700-0799/0768.Max Chunks To Make Sorted II/README_EN.md

+30-8
Original file line numberDiff line numberDiff line change
@@ -130,22 +130,44 @@ func maxChunksToSorted(arr []int) int {
130130

131131
```ts
132132
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();
139139
}
140-
stack.unshift(max);
140+
stack.push(max);
141141
} else {
142-
stack.unshift(num);
142+
stack.push(num);
143143
}
144144
}
145145
return stack.length;
146146
}
147147
```
148148

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+
149171
### **...**
150172

151173
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
}

solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
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();
88
}
9-
stack.unshift(max);
9+
stack.push(max);
1010
} else {
11-
stack.unshift(num);
11+
stack.push(num);
1212
}
1313
}
1414
return stack.length;

solution/0700-0799/0769.Max Chunks To Make Sorted/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,24 @@ function maxChunksToSorted(arr: number[]): number {
149149
}
150150
```
151151

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+
152170
### **...**
153171

154172
```

solution/0700-0799/0769.Max Chunks To Make Sorted/README_EN.md

+18
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,24 @@ function maxChunksToSorted(arr: number[]): number {
133133
}
134134
```
135135

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+
136154
### **...**
137155

138156
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
}

0 commit comments

Comments
 (0)