Skip to content

Commit

Permalink
Add problem 0846: Hand of Straights
Browse files Browse the repository at this point in the history
  • Loading branch information
Spxg committed Jun 27, 2024
1 parent 5cd7ed8 commit c39b07f
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ pub mod problem_0833_find_and_replace_in_string;
pub mod problem_0841_keys_and_rooms;
pub mod problem_0844_backspace_string_compare;
pub mod problem_0845_longest_mountain_in_array;
pub mod problem_0846_hand_of_straights;
pub mod problem_0859_buddy_strings;
pub mod problem_0917_reverse_only_letters;
pub mod problem_2348_number_of_zero_filled_subarrays;
Expand Down
51 changes: 51 additions & 0 deletions src/problem_0846_hand_of_straights/iterative.rs
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>();
}
}
36 changes: 36 additions & 0 deletions src/problem_0846_hand_of_straights/mod.rs
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);
}
}
}

0 comments on commit c39b07f

Please sign in to comment.