Skip to content

Commit dfa92bc

Browse files
committed
double weekly match
1 parent b093c2c commit dfa92bc

File tree

5 files changed

+193
-0
lines changed

5 files changed

+193
-0
lines changed

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,10 @@ mod prob_5355;
394394
mod prob_5356;
395395
mod prob_5357;
396396
mod prob_5359;
397+
mod prob_5360;
398+
mod prob_5361;
399+
mod prob_5362;
400+
mod prob_5363;
397401
mod prob_5364;
398402
mod prob_5366;
399403
mod prob_5367;

src/prob_5360.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn count_largest_group(n: i32) -> i32 {
5+
let mut hash = HashMap::new();
6+
let mut m = 0;
7+
for i in 1..=n {
8+
let mut t = i;
9+
let mut c = 0;
10+
while t > 0 {
11+
c += t%10;
12+
t /= 10;
13+
}
14+
let p = hash.entry(c).or_insert(0);
15+
*p += 1;
16+
m = m.max(*p);
17+
}
18+
let mut ans = 0;
19+
for &v in hash.values() {
20+
if v == m {
21+
ans += 1;
22+
}
23+
}
24+
ans
25+
}
26+
}
27+
28+
struct Solution;

src/prob_5361.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
impl Solution {
2+
pub fn check_overlap(radius: i32, x_center: i32, y_center: i32, x1: i32, y1: i32, x2: i32, y2: i32) -> bool {
3+
if x1<=x_center && y1 <= y_center && x2>=x_center && y2 >= y_center {
4+
return true;
5+
}
6+
if x1 > x_center+radius || x2 < x_center-radius {
7+
return false;
8+
}
9+
if y1 >= y_center {
10+
if y1 > y_center + radius {
11+
false
12+
} else if y1 == y_center + radius {
13+
x1 <= x_center && x2 >= x_center
14+
} else {
15+
let w = radius.pow(2)-(y1-y_center).pow(2);
16+
if x2 < x_center && (x2-x_center).pow(2) > w {
17+
return false;
18+
}
19+
if x1 > x_center && (x1-x_center).pow(2) > w {
20+
return false;
21+
}
22+
true
23+
}
24+
} else if y2 <= y_center {
25+
if y2 < y_center-radius {
26+
false
27+
} else if y2 == y_center-radius {
28+
x1 <= x_center && x2 >= x_center
29+
} else if x1<=x_center-radius && x2>=x_center+radius {
30+
true
31+
} else {
32+
let w = radius.pow(2)-(y2-y_center).pow(2);
33+
if x2 < x_center && (x2-x_center).pow(2) > w {
34+
return false;
35+
}
36+
if x1 > x_center && (x1-x_center).pow(2) > w {
37+
return false;
38+
}
39+
true
40+
}
41+
} else {
42+
true
43+
}
44+
}
45+
}
46+
47+
struct Solution;
48+
49+
#[cfg(test)]
50+
mod tests {
51+
use super::Solution;
52+
53+
#[test]
54+
fn test_check_overlap() {
55+
let test_cases = vec![
56+
(1415,807,-784,-733,623,-533,1005, false),
57+
(1,0,3,7,3,10,6, false),
58+
(1,0,0,1,-1,3,1, true),
59+
(1,0,0,-1,0,0,1, true),
60+
(1,1,1,-3,-3,3,3,true),
61+
(1,1,1,1,-3,2,-1, false),
62+
];
63+
for (r, xc,yc,x1,y1,x2,y2,ok) in test_cases {
64+
assert_eq!(Solution::check_overlap(r,xc,yc,x1,y1,x2,y2), ok, "r:{},xc:{},yc:{},x1:{},y1:{},x2:{},y2:{}", r,xc,yc,x1,y1,x2,y2);
65+
}
66+
}
67+
}

src/prob_5362.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
impl Solution {
2+
pub fn can_construct(s: String, k: i32) -> bool {
3+
let n = s.len();
4+
let k = k as usize;
5+
if n == 0 || n < k {
6+
return false;
7+
}
8+
if n == k {
9+
return true;
10+
}
11+
let mut count = [0; 26];
12+
for &c in s.as_bytes() {
13+
count[(c-b'a') as usize] += 1;
14+
}
15+
let mut odd_num = 0;
16+
for i in 0..26 {
17+
if count[i] & 1 == 1 {
18+
odd_num += 1;
19+
}
20+
}
21+
odd_num <= k
22+
}
23+
}
24+
25+
struct Solution;
26+
27+
#[cfg(test)]
28+
mod tests {
29+
use super::Solution;
30+
31+
#[test]
32+
fn test_can_construct() {
33+
let test_cases = vec![
34+
("leetcode", 3, false),
35+
("yzyzyzyzyzyzyzy", 2, true),
36+
("true", 4, true),
37+
("cr", 7, false),
38+
("annabelle", 2, true),
39+
];
40+
for (s, k, ok) in test_cases {
41+
assert_eq!(Solution::can_construct(s.to_string(), k), ok, "s: {}, k:{}",s,k);
42+
}
43+
}
44+
}

src/prob_5363.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use std::cmp::max;
2+
3+
impl Solution {
4+
pub fn max_satisfaction(mut satisfaction: Vec<i32>) -> i32 {
5+
let n = satisfaction.len();
6+
if n == 0 {
7+
return 0;
8+
}
9+
if n == 1 {
10+
if satisfaction[0] < 0 {
11+
return 0;
12+
}
13+
return satisfaction[0];
14+
}
15+
satisfaction.sort();
16+
let mut s = 0;
17+
let mut w = 0;
18+
let mut ans = 0;
19+
for i in (0..n).rev() {
20+
let val = satisfaction[i]+w+s;
21+
if val < 0 {
22+
break;
23+
}
24+
s += satisfaction[i];
25+
w = val;
26+
ans = ans.max(val);
27+
}
28+
ans
29+
}
30+
}
31+
32+
struct Solution;
33+
34+
#[cfg(test)]
35+
mod tests {
36+
use super::Solution;
37+
38+
#[test]
39+
fn test_max_satisfaction() {
40+
let test_cases = vec![
41+
(vec![-2,5,-1,0,3,-3], 35),
42+
(vec![4,3,2], 20),
43+
(vec![-1,-8,0,5,-9], 14),
44+
(vec![-1,-4,-5], 0),
45+
];
46+
for (sat, expect) in test_cases {
47+
assert_eq!(Solution::max_satisfaction(sat.clone()), expect, "sat: {:?}", sat);
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)