Skip to content

Commit b07b27c

Browse files
committed
weekly contest - dynamic programming is a nightmare, but finally got one
1 parent 38e9fc3 commit b07b27c

File tree

5 files changed

+121
-9
lines changed

5 files changed

+121
-9
lines changed

.idea/workspace.xml

Lines changed: 32 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/dailies/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
//mod d20_12_9;
22
//mod d20_12_10;
33
//mod d20_12_11;
4-
mod d20_12_12;
4+
//mod d20_12_12;

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![allow(dead_code)]
33

44
mod dailies;
5+
mod weeklies;
56
//mod p0001;
67
//mod p0002;
78
//mod p0003;

src/weeklies/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mod w219;

src/weeklies/w219.rs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#[cfg(test)]
2+
mod tests {
3+
use crate::weeklies::w219::p3;
4+
5+
#[test]
6+
fn it_works() {
7+
assert_eq!(6, p3::Solution::stone_game_vii(vec![5,3,1,4,2]));
8+
assert_eq!(122, p3::Solution::stone_game_vii(vec![7,90,5,1,100,10,10,2]));
9+
}
10+
}
11+
12+
mod p1 {
13+
struct Solution;
14+
15+
impl Solution {
16+
pub fn number_of_matches(mut n: i32) -> i32 {
17+
let mut count = 0;
18+
while n > 1 {
19+
count+=n /2;
20+
if n % 2 == 0 {
21+
n /= 2;
22+
} else {
23+
n = n / 2 + 1;
24+
}
25+
}
26+
count
27+
}
28+
}
29+
30+
}
31+
32+
33+
mod p2 {
34+
struct Solution;
35+
impl Solution {
36+
pub fn min_partitions(n: String) -> i32 {
37+
let bytes = n.as_bytes();
38+
let mut max = 0;
39+
for b in bytes {
40+
let i = b - 48;
41+
if i > max{
42+
max = i;
43+
}
44+
}
45+
max as i32
46+
}
47+
}
48+
}
49+
50+
mod p3 {
51+
//problem 1690
52+
pub struct Solution;
53+
54+
impl Solution {
55+
pub fn stone_game_vii(stones: Vec<i32>) -> i32 {
56+
//does not need to be this big, but whatever, it works, first dp done
57+
let mut dp = vec![vec![i32::min_value(); stones.len()]; stones.len()];
58+
for off in 0..stones.len() {
59+
for l in 0..stones.len() {
60+
if l + off == stones.len() {
61+
break;
62+
} else if l == 0 && l + off == stones.len() - 1 {
63+
return Solution::stone_game_score(l, l + off, &stones, &dp);
64+
}
65+
dp[l][l + off] = Solution::stone_game_score(l, l + off, &stones, &dp);
66+
}
67+
}
68+
0
69+
}
70+
71+
fn stone_game_score(l: usize, r: usize, stones: &Vec<i32>, dp: &Vec<Vec<i32>>) -> i32 {
72+
match r - l + 1 {
73+
1 => 0,
74+
2 => stones[l].max(stones[r]),
75+
_ => {
76+
(stones[l..r].iter().sum::<i32>() - dp[l][r - 1])
77+
.max(stones[l + 1..r + 1].iter().sum::<i32>() - dp[l + 1][r])
78+
}
79+
}
80+
}
81+
}
82+
}
83+
84+
mod p4 {
85+
86+
}

0 commit comments

Comments
 (0)