From 06e040e5478f557e1fc4695fe8b987d26289b8dc Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 7 Apr 2025 06:08:50 +0800 Subject: [PATCH 1/2] Update README.md --- .../0416.Partition Equal Subset Sum/README.md | 32 ++++++------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/solution/0400-0499/0416.Partition Equal Subset Sum/README.md b/solution/0400-0499/0416.Partition Equal Subset Sum/README.md index fd4551f364ff0..9aa2ee2cf0214 100644 --- a/solution/0400-0499/0416.Partition Equal Subset Sum/README.md +++ b/solution/0400-0499/0416.Partition Equal Subset Sum/README.md @@ -193,36 +193,24 @@ function canPartition(nums: number[]): boolean { ```rust impl Solution { - #[allow(dead_code)] pub fn can_partition(nums: Vec) -> bool { - let mut sum = 0; - for e in &nums { - sum += *e; - } - - if sum % 2 != 0 { + let s: i32 = nums.iter().sum(); + if s % 2 != 0 { return false; } - + let m = (s / 2) as usize; let n = nums.len(); - let m = (sum / 2) as usize; - let mut dp: Vec> = vec![vec![false; m + 1]; n + 1]; - - // Initialize the dp vector - dp[0][0] = true; - - // Begin the actual dp process + let mut f = vec![vec![false; m + 1]; n + 1]; + f[0][0] = true; + for i in 1..=n { + let x = nums[i - 1] as usize; for j in 0..=m { - dp[i][j] = if (nums[i - 1] as usize) > j { - dp[i - 1][j] - } else { - dp[i - 1][j] || dp[i - 1][j - (nums[i - 1] as usize)] - }; + f[i][j] = f[i - 1][j] || (j >= x && f[i - 1][j - x]); } } - - dp[n][m] + + f[n][m] } } ``` From 9d9fba2d8911b2d40f23b8645ff237d70f2f5b16 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 7 Apr 2025 06:15:28 +0800 Subject: [PATCH 2/2] Refactor Rust `can_partition` function implementation --- .../0416.Partition Equal Subset Sum/README.md | 33 +++------- .../README_EN.md | 65 ++++++------------- .../Solution.rs | 32 +++------ .../Solution2.rs | 33 +++------- 4 files changed, 50 insertions(+), 113 deletions(-) diff --git a/solution/0400-0499/0416.Partition Equal Subset Sum/README.md b/solution/0400-0499/0416.Partition Equal Subset Sum/README.md index 9aa2ee2cf0214..0f7d31e783efc 100644 --- a/solution/0400-0499/0416.Partition Equal Subset Sum/README.md +++ b/solution/0400-0499/0416.Partition Equal Subset Sum/README.md @@ -367,36 +367,23 @@ function canPartition(nums: number[]): boolean { ```rust impl Solution { - #[allow(dead_code)] pub fn can_partition(nums: Vec) -> bool { - let mut sum = 0; - for e in &nums { - sum += *e; - } - - if sum % 2 != 0 { + let s: i32 = nums.iter().sum(); + if s % 2 != 0 { return false; } + let m = (s / 2) as usize; + let mut f = vec![false; m + 1]; + f[0] = true; - let m = (sum >> 1) as usize; - - // Here dp[i] means if it can be sum up to `i` for all the number we've traversed through so far - // Which is actually compressing the 2-D dp vector to 1-D - let mut dp: Vec = vec![false; m + 1]; - - // Initialize the dp vector - dp[0] = true; - - // Begin the actual dp process - for e in &nums { - // For every num in nums vector - for i in (*e as usize..=m).rev() { - // Update the current status - dp[i] |= dp[i - (*e as usize)]; + for x in nums { + let x = x as usize; + for j in (x..=m).rev() { + f[j] = f[j] || f[j - x]; } } - dp[m] + f[m] } } ``` diff --git a/solution/0400-0499/0416.Partition Equal Subset Sum/README_EN.md b/solution/0400-0499/0416.Partition Equal Subset Sum/README_EN.md index 1b1a01a59566d..85c913a408777 100644 --- a/solution/0400-0499/0416.Partition Equal Subset Sum/README_EN.md +++ b/solution/0400-0499/0416.Partition Equal Subset Sum/README_EN.md @@ -192,36 +192,24 @@ function canPartition(nums: number[]): boolean { ```rust impl Solution { - #[allow(dead_code)] pub fn can_partition(nums: Vec) -> bool { - let mut sum = 0; - for e in &nums { - sum += *e; - } - - if sum % 2 != 0 { + let s: i32 = nums.iter().sum(); + if s % 2 != 0 { return false; } - + let m = (s / 2) as usize; let n = nums.len(); - let m = (sum / 2) as usize; - let mut dp: Vec> = vec![vec![false; m + 1]; n + 1]; - - // Initialize the dp vector - dp[0][0] = true; - - // Begin the actual dp process + let mut f = vec![vec![false; m + 1]; n + 1]; + f[0][0] = true; + for i in 1..=n { + let x = nums[i - 1] as usize; for j in 0..=m { - dp[i][j] = if (nums[i - 1] as usize) > j { - dp[i - 1][j] - } else { - dp[i - 1][j] || dp[i - 1][j - (nums[i - 1] as usize)] - }; + f[i][j] = f[i - 1][j] || (j >= x && f[i - 1][j - x]); } } - - dp[n][m] + + f[n][m] } } ``` @@ -378,36 +366,23 @@ function canPartition(nums: number[]): boolean { ```rust impl Solution { - #[allow(dead_code)] pub fn can_partition(nums: Vec) -> bool { - let mut sum = 0; - for e in &nums { - sum += *e; - } - - if sum % 2 != 0 { + let s: i32 = nums.iter().sum(); + if s % 2 != 0 { return false; } + let m = (s / 2) as usize; + let mut f = vec![false; m + 1]; + f[0] = true; - let m = (sum >> 1) as usize; - - // Here dp[i] means if it can be sum up to `i` for all the number we've traversed through so far - // Which is actually compressing the 2-D dp vector to 1-D - let mut dp: Vec = vec![false; m + 1]; - - // Initialize the dp vector - dp[0] = true; - - // Begin the actual dp process - for e in &nums { - // For every num in nums vector - for i in (*e as usize..=m).rev() { - // Update the current status - dp[i] |= dp[i - (*e as usize)]; + for x in nums { + let x = x as usize; + for j in (x..=m).rev() { + f[j] = f[j] || f[j - x]; } } - dp[m] + f[m] } } ``` diff --git a/solution/0400-0499/0416.Partition Equal Subset Sum/Solution.rs b/solution/0400-0499/0416.Partition Equal Subset Sum/Solution.rs index 498f8f59c63ab..a2acff43385bc 100644 --- a/solution/0400-0499/0416.Partition Equal Subset Sum/Solution.rs +++ b/solution/0400-0499/0416.Partition Equal Subset Sum/Solution.rs @@ -1,33 +1,21 @@ impl Solution { - #[allow(dead_code)] pub fn can_partition(nums: Vec) -> bool { - let mut sum = 0; - for e in &nums { - sum += *e; - } - - if sum % 2 != 0 { + let s: i32 = nums.iter().sum(); + if s % 2 != 0 { return false; } - + let m = (s / 2) as usize; let n = nums.len(); - let m = (sum / 2) as usize; - let mut dp: Vec> = vec![vec![false; m + 1]; n + 1]; - - // Initialize the dp vector - dp[0][0] = true; - - // Begin the actual dp process + let mut f = vec![vec![false; m + 1]; n + 1]; + f[0][0] = true; + for i in 1..=n { + let x = nums[i - 1] as usize; for j in 0..=m { - dp[i][j] = if (nums[i - 1] as usize) > j { - dp[i - 1][j] - } else { - dp[i - 1][j] || dp[i - 1][j - (nums[i - 1] as usize)] - }; + f[i][j] = f[i - 1][j] || (j >= x && f[i - 1][j - x]); } } - - dp[n][m] + + f[n][m] } } diff --git a/solution/0400-0499/0416.Partition Equal Subset Sum/Solution2.rs b/solution/0400-0499/0416.Partition Equal Subset Sum/Solution2.rs index b5e1dfb2d9357..a3bacdf80387c 100644 --- a/solution/0400-0499/0416.Partition Equal Subset Sum/Solution2.rs +++ b/solution/0400-0499/0416.Partition Equal Subset Sum/Solution2.rs @@ -1,33 +1,20 @@ impl Solution { - #[allow(dead_code)] pub fn can_partition(nums: Vec) -> bool { - let mut sum = 0; - for e in &nums { - sum += *e; - } - - if sum % 2 != 0 { + let s: i32 = nums.iter().sum(); + if s % 2 != 0 { return false; } + let m = (s / 2) as usize; + let mut f = vec![false; m + 1]; + f[0] = true; - let m = (sum >> 1) as usize; - - // Here dp[i] means if it can be sum up to `i` for all the number we've traversed through so far - // Which is actually compressing the 2-D dp vector to 1-D - let mut dp: Vec = vec![false; m + 1]; - - // Initialize the dp vector - dp[0] = true; - - // Begin the actual dp process - for e in &nums { - // For every num in nums vector - for i in (*e as usize..=m).rev() { - // Update the current status - dp[i] |= dp[i - (*e as usize)]; + for x in nums { + let x = x as usize; + for j in (x..=m).rev() { + f[j] = f[j] || f[j - x]; } } - dp[m] + f[m] } }