Skip to content

Commit 56b1b70

Browse files
committed
Add 3 problems
1 parent f686ba4 commit 56b1b70

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed

src/solution/lc1338.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use crate::solution::Solution;
2+
use std::collections::HashMap;
3+
4+
impl Solution {
5+
pub fn min_set_size(arr: Vec<i32>) -> i32 {
6+
let mut count_map = HashMap::new();
7+
for &val in &arr {
8+
*count_map.entry(val).or_insert(0) += 1;
9+
}
10+
11+
let mut nums: Vec<i32> = count_map.values().cloned().collect();
12+
nums.sort_by(|a, b| b.cmp(a));
13+
let half = (arr.len() / 2) as i32;
14+
let mut res = 0;
15+
let mut sum = 0;
16+
for val in nums {
17+
res += 1;
18+
sum += val;
19+
if sum >= half {
20+
break;
21+
}
22+
}
23+
24+
res
25+
}
26+
}
27+
28+
#[test]
29+
fn test_reduce() {
30+
struct Test {
31+
arr: Vec<i32>,
32+
expected: i32,
33+
}
34+
35+
let tests = vec![
36+
Test {
37+
arr: vec![3, 3, 3, 3, 5, 5, 5, 2, 2, 7],
38+
expected: 2,
39+
},
40+
Test {
41+
arr: vec![7, 7, 7, 7, 7],
42+
expected: 1,
43+
},
44+
];
45+
46+
for t in tests {
47+
let res = Solution::min_set_size(t.arr);
48+
assert_eq!(t.expected, res);
49+
}
50+
}

src/solution/lc1387.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use crate::solution::Solution;
2+
3+
impl Solution {
4+
pub fn get_kth(lo: i32, hi: i32, k: i32) -> i32 {
5+
let mut nums: Vec<i32> = (lo..=hi).collect();
6+
nums.sort_by(|&a, &b| {
7+
let res = Self::compute_weight(a).cmp(&Self::compute_weight(b));
8+
match res {
9+
std::cmp::Ordering::Equal => a.cmp(&b),
10+
_ => res,
11+
}
12+
});
13+
14+
nums[k as usize - 1]
15+
}
16+
17+
fn compute_weight(mut num: i32) -> i32 {
18+
let mut res = 0;
19+
loop {
20+
match num {
21+
1 => break,
22+
n if n % 2 == 0 => num /= 2,
23+
_ => num = 3 * num + 1,
24+
}
25+
res += 1;
26+
}
27+
28+
res
29+
}
30+
}
31+
32+
#[test]
33+
fn test() {
34+
struct Test {
35+
lo: i32,
36+
hi: i32,
37+
k: i32,
38+
expected: i32,
39+
}
40+
41+
let tests = vec![
42+
Test {
43+
lo: 12,
44+
hi: 15,
45+
k: 2,
46+
expected: 13,
47+
},
48+
Test {
49+
lo: 7,
50+
hi: 11,
51+
k: 4,
52+
expected: 7,
53+
},
54+
];
55+
56+
for t in tests {
57+
let res = Solution::get_kth(t.lo, t.hi, t.k);
58+
assert_eq!(t.expected, res);
59+
}
60+
}

src/solution/lc1812.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use crate::solution::Solution;
2+
3+
impl Solution {
4+
pub fn square_is_white(coordinates: String) -> bool {
5+
let x = coordinates.chars().nth(0).unwrap() as i32;
6+
let y = coordinates.chars().nth(1).unwrap() as i32;
7+
8+
x % 2 != y % 2
9+
}
10+
}
11+
12+
#[test]
13+
fn test_lc1812() {
14+
struct Test {
15+
input: String,
16+
expected: bool,
17+
}
18+
19+
let tests = vec![
20+
Test {
21+
input: String::from("a1"),
22+
expected: false,
23+
},
24+
Test {
25+
input: String::from("h3"),
26+
expected: true,
27+
},
28+
];
29+
30+
for test in tests {
31+
let mut res = Solution::square_is_white(test.input);
32+
assert_eq!(test.expected, res);
33+
}
34+
}

src/solution/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ mod lc101;
44
mod lc104;
55
mod lc105;
66
mod lc122;
7+
mod lc1338;
78
mod lc134;
9+
mod lc1387;
810
mod lc14;
911
mod lc151;
1012
mod lc155;
1113
mod lc169;
1214
mod lc1717;
15+
mod lc1812;
1316
mod lc2;
1417
mod lc21;
1518
mod lc215;

0 commit comments

Comments
 (0)