1
Fork 0

Solve first-missing-positive.

This commit is contained in:
Bauke 2022-04-15 23:50:20 +02:00
parent 23f5b6b769
commit a688ae2cd8
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
3 changed files with 51 additions and 0 deletions

View File

@ -0,0 +1,36 @@
pub fn first_missing_positive(mut numbers: Vec<i32>) -> i32 {
let numbers_length = numbers.len();
// Mark all numbers below 1 and above the maximum range as i32::MAX.
for index in 0..numbers_length {
if numbers[index] < 1 || numbers[index] > numbers_length as i32 {
numbers[index] = i32::MAX;
}
}
for index in 0..numbers_length {
// For each index get its number and subtract 1.
let index = numbers[index].abs() as usize - 1;
// If that index then isn't i32::MAX - 1 (ie. negative or above range).
if index as i32 != i32::MAX - 1 {
// Then mark the number for that index as negative.
numbers[index] = -numbers[index].abs();
}
}
// Then the first positive number we find, the index of that number will be
// the first missing positive. And if there are only negative numbers, then
// the next positive number is the total amount of numbers + 1.
numbers
.into_iter()
.enumerate()
.find_map(|(index, number)| {
if number.is_positive() {
Some(index + 1)
} else {
None
}
})
.unwrap_or_else(|| numbers_length + 1) as i32
}

View File

@ -3,6 +3,7 @@ pub mod contains_duplicate;
pub mod contains_duplicate_ii; pub mod contains_duplicate_ii;
pub mod excel_sheet_column_number; pub mod excel_sheet_column_number;
pub mod excel_sheet_column_title; pub mod excel_sheet_column_title;
pub mod first_missing_positive;
pub mod fizz_buzz; pub mod fizz_buzz;
pub mod implement_strstr; pub mod implement_strstr;
pub mod integer_to_roman; pub mod integer_to_roman;

View File

@ -0,0 +1,14 @@
use leetcode::first_missing_positive::first_missing_positive;
use test_case::test_case;
#[test_case(&[1, 2, 0], 3; "example 1")]
#[test_case(&[3, 4, -1, 1], 2; "example 2")]
#[test_case(&[7, 8, 9, 11, 12], 1; "example 3")]
#[test_case(&[1, 3, 2], 4; "maximum")]
#[test_case(&[1, 3, 3], 2; "duplicate")]
#[test_case(&[0], 1; "zero")]
#[test_case(&(0..=500000).into_iter().collect::<Vec<i32>>(), 500001; "massive")]
fn test_first_missing_positive(input: &[i32], expected: i32) {
assert_eq!(first_missing_positive(input.to_vec()), expected);
}