Skip to content

Commit 99036bc

Browse files
author
ruislan
committed
solved q846
1 parent 1513688 commit 99036bc

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/q/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ mod q834;
394394
mod q839;
395395
mod q842;
396396
mod q844;
397+
mod q846;
397398
mod q849;
398399
mod q852;
399400
mod q856;

src/q/q846.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use crate::q::Solution;
2+
3+
#[allow(unused)]
4+
impl Solution {
5+
pub fn is_n_straight_hand(hand: Vec<i32>, group_size: i32) -> bool {
6+
// 方法1
7+
// 排序hand然后,将所有的数字的频率存入hashmap
8+
// 依次迭代hand,如果当前数字没有量了,则继续迭代
9+
// 如果还有量,取出来处理,查看hashmap中是否存在,如果存在,则继续
10+
// 直到达到group_size,并去掉hashmap中的量
11+
// 如果没有达到group_size,则返回失败
12+
// AC 16ms 2.3mb 73/73
13+
use std::collections::HashMap;
14+
let n = hand.len();
15+
let mut hand = hand;
16+
hand.sort_unstable();
17+
Ï
18+
let mut freq = HashMap::new();
19+
for i in 0..n {
20+
*freq.entry(hand[i]).or_insert(0) += 1;
21+
}
22+
for i in 0..n {
23+
let x = hand[i];
24+
let count = freq.get_mut(&x).unwrap();
25+
if *count > 0 {
26+
*count -= 1;
27+
for j in 1..group_size {
28+
let y = x + j;
29+
if let Some(count) = freq.get_mut(&y) {
30+
if *count > 0 {
31+
*count -= 1;
32+
} else {
33+
return false;
34+
}
35+
} else {
36+
return false;
37+
}
38+
}
39+
}
40+
}
41+
true
42+
}
43+
}

0 commit comments

Comments
 (0)