Skip to content

Commit 7f7c3ef

Browse files
committed
Add 768 and 769 solution
1 parent a7e8b29 commit 7f7c3ef

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

src/solution/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ mod s0623_add_one_row_to_tree;
1111
mod s0636_exclusive_time_of_function;
1212
mod s0640_solve_the_equation;
1313
mod s0761_special_binary_string;
14+
mod s0768_max_chunks_to_make_sorted_ii;
15+
mod s0769_max_chunks_to_make_sorted;
1416
mod s0899_orderly_queue;
1517
mod s1282_group_the_people_given_the_group_size_they_belong_to;
1618
mod s1374_generate_a_string_with_characters_that_have_odd_counts;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* [768] max chunks to make sorted II
3+
* https://leetcode.cn/problems/max-chunks-to-make-sorted-ii/
4+
*/
5+
6+
pub struct Solution {}
7+
8+
impl Solution {
9+
pub fn max_chunks_to_sorted(arr: Vec<i32>) -> i32 {
10+
let mut strategy = 0_i32;
11+
let mut sorted_arr = arr.clone();
12+
sorted_arr.sort();
13+
let mut sum_arr = 0;
14+
let mut sum_arr_sorted = 0;
15+
for i in 0..arr.len() {
16+
sum_arr += arr[i];
17+
sum_arr_sorted += sorted_arr[i];
18+
if sum_arr == sum_arr_sorted {
19+
strategy += 1;
20+
}
21+
}
22+
strategy
23+
}
24+
}
25+
26+
#[cfg(test)]
27+
mod tests {
28+
use super::*;
29+
30+
#[test]
31+
fn test_0768() {
32+
assert_eq!(Solution::max_chunks_to_sorted(vec![5, 4, 3, 2, 1]), 1);
33+
assert_eq!(Solution::max_chunks_to_sorted(vec![2, 1, 3, 4, 4]), 4);
34+
}
35+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* [769] max chunks to make sorted
3+
* https://leetcode.cn/problems/max-chunks-to-make-sorted/
4+
*/
5+
6+
pub struct Solution {}
7+
8+
impl Solution {
9+
pub fn max_chunks_to_sorted(arr: Vec<i32>) -> i32 {
10+
let mut strategy = 0_i32;
11+
let mut max = 0_i32;
12+
for (index, number) in arr.iter().enumerate() {
13+
max = max.max(*number);
14+
if (index as i32) == max {
15+
strategy += 1;
16+
}
17+
}
18+
strategy
19+
}
20+
}
21+
22+
#[cfg(test)]
23+
mod tests {
24+
use super::*;
25+
26+
#[test]
27+
fn test_0769() {
28+
assert_eq!(Solution::max_chunks_to_sorted(vec![4, 3, 2, 1, 0]), 1);
29+
assert_eq!(Solution::max_chunks_to_sorted(vec![1, 0, 2, 3, 4]), 4);
30+
}
31+
}

0 commit comments

Comments
 (0)