Skip to content

Commit 3915a11

Browse files
committed
7-21-2020
1 parent 634613a commit 3915a11

8 files changed

+251
-0
lines changed

src/_0496_next_greater_element_i.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
struct Solution;
2+
use std::collections::HashMap;
3+
4+
impl Solution {
5+
pub fn next_greater_element(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<i32> {
6+
let mut h_map: HashMap<i32, i32> = HashMap::new();
7+
let mut h_stack: Vec<i32> = vec![];
8+
9+
for v in nums2 {
10+
while let Some(last) = h_stack.pop() {
11+
if v >= last {
12+
h_map.insert(last, v);
13+
} else {
14+
h_stack.push(last);
15+
break;
16+
}
17+
}
18+
h_stack.push(v);
19+
}
20+
nums1.iter().map(|v| *h_map.get(v).unwrap_or(&-1)).collect()
21+
}
22+
}
23+
24+
#[test]
25+
fn test() {
26+
assert_eq!(
27+
Solution::next_greater_element(vec![4, 1, 2], vec![1, 3, 4, 2]),
28+
vec![-1, 3, -1]
29+
);
30+
assert_eq!(
31+
Solution::next_greater_element(vec![2, 4], vec![1, 2, 3, 4]),
32+
vec![3, -1]
33+
);
34+
}

src/_0503_next_greater_element_ii.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn next_greater_elements(nums: Vec<i32>) -> Vec<i32> {
5+
let n = nums.len();
6+
let mut result: Vec<i32> = vec![-1; n];
7+
let mut h_stack: Vec<usize> = vec![];
8+
9+
for i in 0..2 * n {
10+
let i_: usize = i % n;
11+
while let Some(last) = h_stack.pop() {
12+
if nums[i_] > nums[last] {
13+
result[last] = nums[i_] as i32;
14+
} else {
15+
h_stack.push(last);
16+
break;
17+
}
18+
}
19+
h_stack.push(i_);
20+
}
21+
result
22+
}
23+
}
24+
25+
#[test]
26+
fn test() {
27+
assert_eq!(
28+
Solution::next_greater_elements(vec![1, 2, 1]),
29+
vec![2, -1, 2]
30+
);
31+
}

src/_0556_next_greater_element_iii.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn next_greater_element(n: i32) -> i32 {
5+
let mut num: i32 = n.clone();
6+
let mut last: i32 = num % 10;
7+
let mut h_array: Vec<i32> = vec![last];
8+
num /= 10;
9+
10+
while num > 0 {
11+
let mut current = num % 10;
12+
if last <= current {
13+
h_array.push(current);
14+
} else {
15+
let h_array_len: usize = h_array.len();
16+
let mut num_replace: i32 = h_array[h_array_len - 1];
17+
let mut index_replace: usize = h_array_len - 1;
18+
for i in 0..h_array_len {
19+
if h_array[i] > current && h_array[i] <= num_replace {
20+
num_replace = h_array[i].clone();
21+
index_replace = i;
22+
break;
23+
}
24+
}
25+
h_array[index_replace] = current;
26+
current = num_replace;
27+
num = num / 10 * 10 + current;
28+
for v in h_array.iter() {
29+
if num as i64 * 10 + *v as i64 > std::i32::MAX as i64 {
30+
return -1;
31+
} else {
32+
num = num * 10 + v;
33+
}
34+
}
35+
return num;
36+
}
37+
last = current;
38+
num /= 10;
39+
}
40+
-1
41+
}
42+
}
43+
44+
#[test]
45+
fn test() {
46+
assert_eq!(Solution::next_greater_element(12), 21);
47+
assert_eq!(Solution::next_greater_element(21), -1);
48+
assert_eq!(Solution::next_greater_element(1999999999), -1);
49+
}

src/_0561_array_partition_i.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn array_pair_sum(mut nums: Vec<i32>) -> i32 {
5+
nums.sort_unstable();
6+
nums.chunks(2).fold(0, |sum, pair| sum + pair[0])
7+
}
8+
}
9+
#[test]
10+
fn test() {
11+
assert_eq!(Solution::array_pair_sum(vec![1, 4, 3, 2]), 4);
12+
}

src/_0905_sort_array_by_parity.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn sort_array_by_parity(mut a: Vec<i32>) -> Vec<i32> {
5+
let mut even: usize = 0;
6+
let mut odd: usize = a.len() - 1;
7+
while even < odd {
8+
while a[even] % 2 == 0 && even < odd {
9+
even += 1;
10+
}
11+
while a[odd] % 2 == 1 && even < odd {
12+
odd -= 1;
13+
}
14+
if even < odd {
15+
a.swap(even, odd);
16+
}
17+
}
18+
a
19+
}
20+
}
21+
22+
#[test]
23+
fn test() {
24+
assert_eq!(
25+
Solution::sort_array_by_parity(vec![3, 1, 2, 4]),
26+
vec![4, 2, 1, 3]
27+
);
28+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn can_three_parts_equal_sum(a: Vec<i32>) -> bool {
5+
let sum_total: i32 = a.iter().sum();
6+
if sum_total % 3 != 0 {
7+
return false;
8+
}
9+
let mut count: i32 = 0;
10+
let mut sum_temp: i32 = 0;
11+
for v in a.iter() {
12+
sum_temp += v;
13+
if sum_temp == sum_total / 3 {
14+
count += 1;
15+
sum_temp = 0;
16+
}
17+
if count == 3 {
18+
return true;
19+
}
20+
}
21+
false
22+
}
23+
}
24+
25+
#[test]
26+
fn test() {
27+
assert_eq!(
28+
Solution::can_three_parts_equal_sum(vec![0, 2, 1, -6, 6, -7, 9, 1, 2, 0, 1]),
29+
true
30+
);
31+
assert_eq!(
32+
Solution::can_three_parts_equal_sum(vec![0, 2, 1, -6, 6, 7, 9, -1, 2, 0, 1]),
33+
false
34+
);
35+
assert_eq!(
36+
Solution::can_three_parts_equal_sum(vec![3, 3, 6, 5, -2, 2, 5, 1, -9, 4]),
37+
true
38+
);
39+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn min_remove_to_make_valid(s: String) -> String {
5+
let mut characters: Vec<char> = s.chars().collect::<Vec<char>>();
6+
let mut h_stack: Vec<usize> = vec![];
7+
for i in 0..characters.len() {
8+
match characters[i] {
9+
')' => match h_stack.pop() {
10+
None => characters[i] = '_',
11+
_ => {}
12+
},
13+
'(' => {
14+
h_stack.push(i);
15+
}
16+
_ => {}
17+
}
18+
}
19+
while let Some(index) = h_stack.pop() {
20+
characters[index] = '_';
21+
}
22+
characters.iter().filter(|&x| *x != '_').collect::<String>()
23+
}
24+
}
25+
26+
#[test]
27+
fn test() {
28+
assert_eq!(
29+
Solution::min_remove_to_make_valid("lee(t(c)o)de)".to_string()),
30+
"lee(t(c)o)de".to_string()
31+
);
32+
assert_eq!(
33+
Solution::min_remove_to_make_valid("a)b(c)d".to_string()),
34+
"ab(c)d".to_string()
35+
);
36+
assert_eq!(
37+
Solution::min_remove_to_make_valid("))((".to_string()),
38+
"".to_string()
39+
);
40+
assert_eq!(
41+
Solution::min_remove_to_make_valid("(a(b(c)d)".to_string()),
42+
"a(b(c)d)".to_string()
43+
);
44+
}

src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,20 @@ mod _0415_add_strings;
100100
//
101101
mod _0448_find_all_numbers_disappeared_in_an_array;
102102
//
103+
mod _0496_next_greater_element_i;
104+
//
105+
mod _0503_next_greater_element_ii;
106+
//
103107
mod _0509_fibonacci_number;
104108
//
109+
mod _0556_next_greater_element_iii;
110+
//
105111
mod _0557_reverse_words_in_a_string_iii;
106112
//
107113
mod _0560_subarray_sum_equals_k;
108114
//
115+
mod _0561_array_partition_i;
116+
//
109117
mod _0605_can_place_flowers;
110118
//
111119
mod _0680_valid_palindrome_ii;
@@ -120,12 +128,18 @@ mod _0796_rotate_string;
120128
//
121129
mod _0844_backspace_string_compare;
122130
//
131+
mod _0905_sort_array_by_parity;
132+
//
123133
mod _0953_verifying_an_alien_dictionary;
124134
//
125135
mod _0977_squares_of_a_sorted_array;
126136
//
137+
mod _1013_partition_array_into_three_parts_with_equal_sum;
138+
//
127139
mod _1029_two_city_scheduling;
128140
//
141+
mod _1249_minimum_remove_to_make_valid_parentheses;
142+
//
129143
mod _1281_subtract_the_product_and_sum_of_digits_of_an_integer;
130144
//
131145
mod _1287_element_appearing_more_than_25_in_sorted_array;

0 commit comments

Comments
 (0)