Skip to content

Commit 4fb3a7d

Browse files
committed
solve(2996): implement solution using hash set
1 parent 97bd66f commit 4fb3a7d

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use std::collections::HashSet;
2+
3+
use crate::solutions::Solution;
4+
5+
impl Solution {
6+
pub fn missing_integer(nums: Vec<i32>) -> i32 {
7+
let prefix_len = 1 + nums
8+
.windows(2)
9+
.take_while(|pair| pair[1] == pair[0] + 1)
10+
.count();
11+
12+
let prefix_sum = nums.iter().take(prefix_len).sum::<i32>();
13+
14+
let hash_set: HashSet<i32> = nums.iter().cloned().collect();
15+
16+
(prefix_sum..)
17+
.find(|x| !hash_set.contains(x))
18+
.unwrap_or(prefix_sum)
19+
}
20+
}
21+
22+
#[test]
23+
fn test() {
24+
assert_eq!(Solution::missing_integer(vec![1, 2, 3, 2, 5]), 6);
25+
assert_eq!(Solution::missing_integer(vec![3, 4, 5, 1, 12, 14, 13]), 15);
26+
}

src/solutions/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub mod _0027_remove_element;
66
pub mod _0929_unique_email_addresses;
77
pub mod _1848_minimum_distance_to_the_target_element;
88
pub mod _2351_first_letter_to_appear_twice;
9+
pub mod _2996_smallest_missing_integer_greater_than_sequential_prefix_sum;
910
pub mod _3046_split_the_array;
1011
pub mod _3151_special_array_i;
1112
pub mod _3174_clear_digits;

0 commit comments

Comments
 (0)