Skip to content

Commit aece3d3

Browse files
committed
1140&753
1 parent 92d388d commit aece3d3

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ mod prob_732;
307307
mod prob_735;
308308
mod prob_739;
309309
mod prob_751;
310+
mod prob_753;
310311
mod prob_756;
311312
mod prob_765;
312313
mod prob_785;
@@ -333,6 +334,7 @@ mod prob_1059;
333334
mod prob_1092;
334335
mod prob_1109;
335336
mod prob_1135;
337+
mod prob_1140;
336338
mod prob_1157;
337339
mod prob_1167;
338340
mod prob_1228;

src/prob_1140.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
impl Solution {
2+
pub fn stone_game_ii(piles: Vec<i32>) -> i32 {
3+
let n = piles.len();
4+
if n <= 2 {
5+
return piles.iter().sum();
6+
}
7+
let mut sum = vec![0; n+1];
8+
for i in 0..n {
9+
sum[i+1] = sum[i]+piles[i];
10+
}
11+
let mut f = vec![vec![0; n]; n];
12+
Self::dfs(0, 1, &sum, &mut f)
13+
}
14+
fn dfs(idx: usize, m: usize, sum: &Vec<i32>, f: &mut Vec<Vec<i32>>) -> i32 {
15+
let n = f.len();
16+
if idx >= n {
17+
return 0;
18+
}
19+
if f[idx][m] > 0 {
20+
return f[idx][m];
21+
}
22+
let mut ans = 0;
23+
for i in idx..n.min(idx+2*m) {
24+
let v = sum[n]-sum[idx] - Self::dfs(i+1, m.max(i-idx+1), sum, f);
25+
ans = ans.max(v);
26+
}
27+
f[idx][m] = ans;
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_stone_game_ii() {
40+
let test_cases = vec![
41+
(vec![2,7,9,4,4], 10),
42+
];
43+
for (piles, expect) in test_cases {
44+
assert_eq!(Solution::stone_game_ii(piles.clone()), expect, "piles: {:?}", piles);
45+
}
46+
}
47+
}

src/prob_753.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use std::collections::HashSet;
2+
3+
impl Solution {
4+
pub fn crack_safe(n: i32, k: i32) -> String {
5+
if n == 1 && k == 1 {
6+
return "0".to_string();
7+
}
8+
let mut ans = vec![];
9+
let mut hash = HashSet::new();
10+
let mut m = 1;
11+
while 2i32.pow(m) < k { m+=1;}
12+
let w = 2i32.pow(m)-1;
13+
let mut mask = 0;
14+
for _ in 0..n-1 {
15+
mask = (mask << m) | w;
16+
}
17+
Self::dfs(0, k, mask, m, &mut hash, &mut ans);
18+
for _ in 0..n-1 {
19+
ans.push(b'0');
20+
}
21+
unsafe {std::str::from_utf8_unchecked(&ans).to_string()}
22+
}
23+
fn dfs(node: i32, k: i32, mask: i32, m: u32, hash: &mut HashSet<i32>, ans: &mut Vec<u8>) {
24+
for i in 0..k {
25+
let w = (node << m) | i;
26+
if !hash.contains(&w) {
27+
hash.insert(w);
28+
Self::dfs(w & mask, k, mask, m, hash, ans);
29+
ans.push(b'0'+i as u8);
30+
}
31+
}
32+
}
33+
}
34+
35+
struct Solution;
36+
37+
#[cfg(test)]
38+
mod tests {
39+
use super::Solution;
40+
41+
#[test]
42+
fn test_crack_safe() {
43+
let test_cases = vec![
44+
(1,2,"10"),
45+
(2,2,"01100"),
46+
];
47+
for (n,k,expect) in test_cases {
48+
assert_eq!(Solution::crack_safe(n,k), expect, "n:{},k:{}",n,k);
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)