-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
88 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; | ||
|
||
use std::collections::HashMap; | ||
|
||
impl Solution { | ||
pub fn is_n_straight_hand(hand: Vec<i32>, group_size: i32) -> bool { | ||
let group_size = group_size as usize; | ||
if hand.len() % group_size != 0 { | ||
return false; | ||
} | ||
let mut map = HashMap::with_capacity(hand.len()); | ||
for &num in &hand { | ||
*map.entry(num).or_insert(0) += 1; | ||
} | ||
let mut map = map.into_iter().collect::<Vec<_>>(); | ||
map.sort_unstable(); | ||
|
||
for idx in 0..map.len() { | ||
let num = map[idx]; | ||
if num.1 == 0 { | ||
continue; | ||
} | ||
for offset in 0..group_size { | ||
let target_idx = offset + idx; | ||
if target_idx >= map.len() { | ||
return false; | ||
} | ||
let target = map[target_idx]; | ||
if num.0 + offset as i32 != target.0 || target.1 < num.1 { | ||
return false; | ||
} | ||
map[target_idx] = (target.0, target.1 - num.1); | ||
} | ||
} | ||
map.into_iter().all(|(_, count)| count == 0) | ||
} | ||
} | ||
|
||
impl super::Solution for Solution { | ||
fn is_n_straight_hand(hand: Vec<i32>, group_size: i32) -> bool { | ||
Self::is_n_straight_hand(hand, group_size) | ||
} | ||
} | ||
|
||
#[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,36 @@ | ||
pub mod iterative; | ||
|
||
pub trait Solution { | ||
fn is_n_straight_hand(hand: Vec<i32>, group_size: i32) -> bool; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
((&[1, 2, 3, 6, 2, 3, 4, 7, 8] as &[_], 3), true), | ||
((&[1, 2, 3, 4, 5], 4), false), | ||
((&[0, 0], 2), false), | ||
( | ||
( | ||
&[ | ||
9, 13, 15, 23, 22, 25, 4, 4, 29, 15, 8, 23, 12, 19, 24, 17, 18, 11, 22, 24, | ||
17, 17, 10, 23, 21, 18, 14, 18, 7, 6, 3, 6, 19, 11, 16, 11, 12, 13, 8, 26, | ||
17, 20, 13, 19, 22, 21, 27, 9, 20, 15, 20, 27, 8, 13, 25, 23, 22, 15, 9, | ||
14, 20, 10, 6, 5, 14, 12, 7, 16, 21, 18, 21, 24, 23, 10, 21, 16, 18, 16, | ||
18, 5, 20, 19, 20, 10, 14, 26, 2, 9, 19, 12, 28, 17, 5, 7, 25, 22, 16, 17, | ||
21, 11, | ||
], | ||
10, | ||
), | ||
false, | ||
), | ||
]; | ||
|
||
for ((hand, group_size), expected) in test_cases { | ||
assert_eq!(S::is_n_straight_hand(hand.to_vec(), group_size), expected); | ||
} | ||
} | ||
} |