leet-code/first-missing-positive/sol.rs

20 lines
852 B
Rust
Raw Normal View History

2022-05-29 19:21:38 +00:00
// TODO: Incomplete
struct Solution {}
impl Solution {
pub fn first_missing_positive(nums: Vec<i32>) -> i32 {
let (max, sum) = nums
.iter()
.fold((0, 0), |(max, sum), n| {
if *n > 0 {
return (if *n > max { *n } else { max }, sum + *n);
}
(max, sum)
});
let missing = max * (max + 1) / 2 - sum;
return if missing == 0 { max + 1 } else { missing };
}
}
fn main() {
println!("{}", Solution::first_missing_positive(vec![99,94,96,11,92,5,91,89,57,85,66,63,84,81,79,61,74,78,77,30,64,13,58,18,70,69,51,12,32,34,9,43,39,8,1,38,49,27,21,45,47,44,53,52,48,19,50,59,3,40,31,82,23,56,37,41,16,28,22,33,65,42,54,20,29,25,10,26,4,60,67,83,62,71,24,35,72,55,75,0,2,46,15,80,6,36,14,73,76,86,88,7,17,87,68,90,95,93,97,98]));
}