-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 1854: Maximum Population Year
- Loading branch information
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::convert::TryInto; | ||
|
||
impl Solution { | ||
pub fn maximum_population(logs: Vec<Vec<i32>>) -> i32 { | ||
const MIN_YEAR: usize = 1950; | ||
|
||
let mut diffs = [0_i8; 101]; | ||
|
||
for log in logs { | ||
let [birth, death]: [_; 2] = log.try_into().ok().unwrap(); | ||
|
||
diffs[birth as u32 as usize - MIN_YEAR] += 1; | ||
diffs[death as u32 as usize - MIN_YEAR] -= 1; | ||
} | ||
|
||
let mut max_population = 0; | ||
let mut max_population_year = 0; | ||
let mut population = 0; | ||
|
||
for (year, diff) in (MIN_YEAR..).zip(diffs) { | ||
population += diff; | ||
|
||
if population > max_population { | ||
max_population = population; | ||
max_population_year = year; | ||
} | ||
} | ||
|
||
max_population_year as _ | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn maximum_population(logs: Vec<Vec<i32>>) -> i32 { | ||
Self::maximum_population(logs) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
pub mod iterative; | ||
|
||
pub trait Solution { | ||
fn maximum_population(logs: Vec<Vec<i32>>) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
(&[[1993, 1999], [2000, 2010]] as &[_], 1993), | ||
(&[[1950, 1961], [1960, 1971], [1970, 1981]], 1960), | ||
]; | ||
|
||
for (logs, expected) in test_cases { | ||
assert_eq!( | ||
S::maximum_population(logs.iter().copied().map(Vec::from).collect()), | ||
expected | ||
); | ||
} | ||
} | ||
} |