2022-09-25 09:12:58 +00:00
|
|
|
pub fn first_missing_positive(numbers: Vec<i32>) -> i32 {
|
2022-04-15 21:50:20 +00:00
|
|
|
let numbers_length = numbers.len();
|
|
|
|
|
|
|
|
// Mark all numbers below 1 and above the maximum range as i32::MAX.
|
2022-09-25 09:12:58 +00:00
|
|
|
let mut numbers = numbers
|
|
|
|
.into_iter()
|
|
|
|
.map(|number| {
|
|
|
|
if number < 1 || number > numbers_length as i32 {
|
|
|
|
i32::MAX
|
|
|
|
} else {
|
|
|
|
number
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2022-04-15 21:50:20 +00:00
|
|
|
|
|
|
|
for index in 0..numbers_length {
|
|
|
|
// For each index get its number and subtract 1.
|
2022-09-25 09:12:58 +00:00
|
|
|
let index = numbers[index].unsigned_abs() as usize - 1;
|
2022-04-15 21:50:20 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
})
|
2022-09-25 09:12:58 +00:00
|
|
|
.unwrap_or(numbers_length + 1) as i32
|
2022-04-15 21:50:20 +00:00
|
|
|
}
|