diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index da03c1c1b2..0000000000 Binary files a/.DS_Store and /dev/null differ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000..6a3e68da15 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +**/.DS_Store \ No newline at end of file diff --git "a/problems/0343.346円225円264円346円225円260円346円213円206円345円210円206円.md" "b/problems/0343.346円225円264円346円225円260円346円213円206円345円210円206円.md" index 812bf67bec..ddd6f16258 100644 --- "a/problems/0343.346円225円264円346円225円260円346円213円206円345円210円206円.md" +++ "b/problems/0343.346円225円264円346円225円260円346円213円206円345円210円206円.md" @@ -319,6 +319,29 @@ pub fn integer_break(n: i32) -> i32 { } ``` +贪心: + +```rust +impl Solution { + pub fn integer_break(mut n: i32) -> i32 { + match n { + 2 => 1, + 3 => 2, + 4 => 4, + 5.. => { + let mut res = 1; + while n> 4 { + res *= 3; + n -= 3; + } + res * n + } + _ => panic!("Error"), + } + } +} +``` + ### TypeScript ```typescript @@ -344,27 +367,6 @@ function integerBreak(n: number): number { }; ``` -### Rust - -```Rust -impl Solution { - fn max(a: i32, b: i32) -> i32{ - if a> b { a } else { b } - } - pub fn integer_break(n: i32) -> i32 { - let n = n as usize; - let mut dp = vec![0; n + 1]; - dp[2] = 1; - for i in 3..=n { - for j in 1..i - 1 { - dp[i] = Self::max(dp[i], Self::max(((i - j) * j) as i32, dp[i - j] * j as i32)); - } - } - dp[n] - } -} -``` - ### C ```c diff --git "a/problems/350円203円214円345円214円205円347円220円206円350円256円272円345円237円272円347円241円20001円350円203円214円345円214円205円-1.md" "b/problems/350円203円214円345円214円205円347円220円206円350円256円272円345円237円272円347円241円20001円350円203円214円345円214円205円-1.md" index c45fc3d302..c25252488a 100644 --- "a/problems/350円203円214円345円214円205円347円220円206円350円256円272円345円237円272円347円241円20001円350円203円214円345円214円205円-1.md" +++ "b/problems/350円203円214円345円214円205円347円220円206円350円256円272円345円237円272円347円241円20001円350円203円214円345円214円205円-1.md" @@ -573,6 +573,39 @@ object Solution { } ``` +### Rust + +```rust +pub struct Solution; + +impl Solution { + pub fn wei_bag_problem1(weight: Vec, value: Vec, bag_size: usize) -> usize { + let mut dp = vec![vec![0; bag_size + 1]; weight.len()]; + for j in weight[0]..=weight.len() { + dp[0][j] = value[0]; + } + + for i in 1..weight.len() { + for j in 0..=bag_size { + match j < weight[i] { + true => dp[i][j] = dp[i - 1][j], + false => dp[i][j] = dp[i - 1][j].max(dp[i - 1][j - weight[i]] + value[i]), + } + } + } + dp[weight.len() - 1][bag_size] + } +} + +#[test] +fn test_wei_bag_problem1() { + println!( + "{}", + Solution::wei_bag_problem1(vec![1, 3, 4], vec![15, 20, 30], 4) + ); +} +``` +

diff --git "a/problems/350円203円214円345円214円205円347円220円206円350円256円272円345円237円272円347円241円20001円350円203円214円345円214円205円-2.md" "b/problems/350円203円214円345円214円205円347円220円206円350円256円272円345円237円272円347円241円20001円350円203円214円345円214円205円-2.md" index baa4107f58..3b798334a6 100644 --- "a/problems/350円203円214円345円214円205円347円220円206円350円256円272円345円237円272円347円241円20001円350円203円214円345円214円205円-2.md" +++ "b/problems/350円203円214円345円214円205円347円220円206円350円256円272円345円237円272円347円241円20001円350円203円214円345円214円205円-2.md" @@ -406,6 +406,34 @@ object Solution { } ``` +### Rust + +```rust +pub struct Solution; + +impl Solution { + pub fn wei_bag_problem2(weight: Vec, value: Vec, bag_size: usize) -> usize { + let mut dp = vec![0; bag_size + 1]; + for i in 0..weight.len() { + for j in (weight[i]..=bag_size).rev() { + if j>= weight[i] { + dp[j] = dp[j].max(dp[j - weight[i]] + value[i]); + } + } + } + dp[dp.len() - 1] + } +} + +#[test] +fn test_wei_bag_problem2() { + println!( + "{}", + Solution::wei_bag_problem2(vec![1, 3, 4], vec![15, 20, 30], 4) + ); +} +``` +

AltStyle によって変換されたページ (->オリジナル) /