Skip to content

Commit

Permalink
leetcode: Add 0846
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed May 9, 2024
1 parent ae3b5ea commit ab5db43
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/leetcode/0846.hand-of-straights/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "lc-0846-hand-of-straights"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
4 changes: 4 additions & 0 deletions src/leetcode/0846.hand-of-straights/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

#

[问题描述](../problems/)
77 changes: 77 additions & 0 deletions src/leetcode/0846.hand-of-straights/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright (c) 2024 Xu Shaohua <[email protected]>. All rights reserved.
// Use of this source is governed by General Public License that can be found
// in the LICENSE file.

use std::collections::BTreeMap;

// BTreeMap
// 计数, 并保证 key 是有序的
pub fn is_n_straight_hand1(hand: Vec<i32>, group_size: i32) -> bool {
let nums = hand;
let k = group_size;

assert!(k >= 1 && k as usize <= nums.len());
let k_usize = k as usize;

// 处理边角情况.
if nums.len() % k_usize != 0 {
return false;
}

let mut nums = nums;
nums.sort_unstable();

let mut map: BTreeMap<i32, usize> = BTreeMap::new();
for &num in &nums {
*map.entry(num).or_default() += 1;
}

// 遍历所有的整数.
for &num in &nums {
// 计数到0, 用完了.
if map[&num] == 0 {
continue;
}
for offset in 0..k {
let next_num: i32 = offset + num;
match map.get_mut(&next_num) {
Some(count) if *count > 0 => {
*count -= 1;
}
// 计数到0, 用完了;
// 或者整数就不存在, 无法成组
_ => return false,
}
}
}

true
}

// TODO(Shaohua): BinaryHeap

pub type SolutionFn = fn(Vec<i32>, i32) -> bool;

fn check_solution(func: SolutionFn) {
let hand = vec![1, 2, 3, 6, 2, 3, 4, 7, 8];
let group_size = 3;
assert!(func(hand, group_size));

let hand = vec![1, 2, 3, 4, 5];
let group_size = 4;
assert!(!func(hand, group_size));
}

fn main() {
check_solution(is_n_straight_hand1);
}

#[cfg(test)]
mod tests {
use super::{check_solution, is_n_straight_hand1};

#[test]
fn test_is_n_straight_hand1() {
check_solution(is_n_straight_hand1);
}
}

0 comments on commit ab5db43

Please sign in to comment.