Skip to content

Commit 9d9fba2

Browse files
authored
Refactor Rust can_partition function implementation
1 parent 06e040e commit 9d9fba2

File tree

4 files changed

+50
-113
lines changed

4 files changed

+50
-113
lines changed

solution/0400-0499/0416.Partition Equal Subset Sum/README.md

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -367,36 +367,23 @@ function canPartition(nums: number[]): boolean {
367367

368368
```rust
369369
impl Solution {
370-
#[allow(dead_code)]
371370
pub fn can_partition(nums: Vec<i32>) -> bool {
372-
let mut sum = 0;
373-
for e in &nums {
374-
sum += *e;
375-
}
376-
377-
if sum % 2 != 0 {
371+
let s: i32 = nums.iter().sum();
372+
if s % 2 != 0 {
378373
return false;
379374
}
375+
let m = (s / 2) as usize;
376+
let mut f = vec![false; m + 1];
377+
f[0] = true;
380378

381-
let m = (sum >> 1) as usize;
382-
383-
// Here dp[i] means if it can be sum up to `i` for all the number we've traversed through so far
384-
// Which is actually compressing the 2-D dp vector to 1-D
385-
let mut dp: Vec<bool> = vec![false; m + 1];
386-
387-
// Initialize the dp vector
388-
dp[0] = true;
389-
390-
// Begin the actual dp process
391-
for e in &nums {
392-
// For every num in nums vector
393-
for i in (*e as usize..=m).rev() {
394-
// Update the current status
395-
dp[i] |= dp[i - (*e as usize)];
379+
for x in nums {
380+
let x = x as usize;
381+
for j in (x..=m).rev() {
382+
f[j] = f[j] || f[j - x];
396383
}
397384
}
398385

399-
dp[m]
386+
f[m]
400387
}
401388
}
402389
```

solution/0400-0499/0416.Partition Equal Subset Sum/README_EN.md

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -192,36 +192,24 @@ function canPartition(nums: number[]): boolean {
192192

193193
```rust
194194
impl Solution {
195-
#[allow(dead_code)]
196195
pub fn can_partition(nums: Vec<i32>) -> bool {
197-
let mut sum = 0;
198-
for e in &nums {
199-
sum += *e;
200-
}
201-
202-
if sum % 2 != 0 {
196+
let s: i32 = nums.iter().sum();
197+
if s % 2 != 0 {
203198
return false;
204199
}
205-
200+
let m = (s / 2) as usize;
206201
let n = nums.len();
207-
let m = (sum / 2) as usize;
208-
let mut dp: Vec<Vec<bool>> = vec![vec![false; m + 1]; n + 1];
209-
210-
// Initialize the dp vector
211-
dp[0][0] = true;
212-
213-
// Begin the actual dp process
202+
let mut f = vec![vec![false; m + 1]; n + 1];
203+
f[0][0] = true;
204+
214205
for i in 1..=n {
206+
let x = nums[i - 1] as usize;
215207
for j in 0..=m {
216-
dp[i][j] = if (nums[i - 1] as usize) > j {
217-
dp[i - 1][j]
218-
} else {
219-
dp[i - 1][j] || dp[i - 1][j - (nums[i - 1] as usize)]
220-
};
208+
f[i][j] = f[i - 1][j] || (j >= x && f[i - 1][j - x]);
221209
}
222210
}
223-
224-
dp[n][m]
211+
212+
f[n][m]
225213
}
226214
}
227215
```
@@ -378,36 +366,23 @@ function canPartition(nums: number[]): boolean {
378366

379367
```rust
380368
impl Solution {
381-
#[allow(dead_code)]
382369
pub fn can_partition(nums: Vec<i32>) -> bool {
383-
let mut sum = 0;
384-
for e in &nums {
385-
sum += *e;
386-
}
387-
388-
if sum % 2 != 0 {
370+
let s: i32 = nums.iter().sum();
371+
if s % 2 != 0 {
389372
return false;
390373
}
374+
let m = (s / 2) as usize;
375+
let mut f = vec![false; m + 1];
376+
f[0] = true;
391377

392-
let m = (sum >> 1) as usize;
393-
394-
// Here dp[i] means if it can be sum up to `i` for all the number we've traversed through so far
395-
// Which is actually compressing the 2-D dp vector to 1-D
396-
let mut dp: Vec<bool> = vec![false; m + 1];
397-
398-
// Initialize the dp vector
399-
dp[0] = true;
400-
401-
// Begin the actual dp process
402-
for e in &nums {
403-
// For every num in nums vector
404-
for i in (*e as usize..=m).rev() {
405-
// Update the current status
406-
dp[i] |= dp[i - (*e as usize)];
378+
for x in nums {
379+
let x = x as usize;
380+
for j in (x..=m).rev() {
381+
f[j] = f[j] || f[j - x];
407382
}
408383
}
409384

410-
dp[m]
385+
f[m]
411386
}
412387
}
413388
```
Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,21 @@
11
impl Solution {
2-
#[allow(dead_code)]
32
pub fn can_partition(nums: Vec<i32>) -> bool {
4-
let mut sum = 0;
5-
for e in &nums {
6-
sum += *e;
7-
}
8-
9-
if sum % 2 != 0 {
3+
let s: i32 = nums.iter().sum();
4+
if s % 2 != 0 {
105
return false;
116
}
12-
7+
let m = (s / 2) as usize;
138
let n = nums.len();
14-
let m = (sum / 2) as usize;
15-
let mut dp: Vec<Vec<bool>> = vec![vec![false; m + 1]; n + 1];
16-
17-
// Initialize the dp vector
18-
dp[0][0] = true;
19-
20-
// Begin the actual dp process
9+
let mut f = vec![vec![false; m + 1]; n + 1];
10+
f[0][0] = true;
11+
2112
for i in 1..=n {
13+
let x = nums[i - 1] as usize;
2214
for j in 0..=m {
23-
dp[i][j] = if (nums[i - 1] as usize) > j {
24-
dp[i - 1][j]
25-
} else {
26-
dp[i - 1][j] || dp[i - 1][j - (nums[i - 1] as usize)]
27-
};
15+
f[i][j] = f[i - 1][j] || (j >= x && f[i - 1][j - x]);
2816
}
2917
}
30-
31-
dp[n][m]
18+
19+
f[n][m]
3220
}
3321
}
Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,20 @@
11
impl Solution {
2-
#[allow(dead_code)]
32
pub fn can_partition(nums: Vec<i32>) -> bool {
4-
let mut sum = 0;
5-
for e in &nums {
6-
sum += *e;
7-
}
8-
9-
if sum % 2 != 0 {
3+
let s: i32 = nums.iter().sum();
4+
if s % 2 != 0 {
105
return false;
116
}
7+
let m = (s / 2) as usize;
8+
let mut f = vec![false; m + 1];
9+
f[0] = true;
1210

13-
let m = (sum >> 1) as usize;
14-
15-
// Here dp[i] means if it can be sum up to `i` for all the number we've traversed through so far
16-
// Which is actually compressing the 2-D dp vector to 1-D
17-
let mut dp: Vec<bool> = vec![false; m + 1];
18-
19-
// Initialize the dp vector
20-
dp[0] = true;
21-
22-
// Begin the actual dp process
23-
for e in &nums {
24-
// For every num in nums vector
25-
for i in (*e as usize..=m).rev() {
26-
// Update the current status
27-
dp[i] |= dp[i - (*e as usize)];
11+
for x in nums {
12+
let x = x as usize;
13+
for j in (x..=m).rev() {
14+
f[j] = f[j] || f[j - x];
2815
}
2916
}
3017

31-
dp[m]
18+
f[m]
3219
}
3320
}

0 commit comments

Comments
 (0)