Skip to content

Commit ba060d5

Browse files
authored
feat: update solution to lc problem: No.0416 (#4334)
1 parent c5bfe3f commit ba060d5

File tree

4 files changed

+60
-135
lines changed

4 files changed

+60
-135
lines changed

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

+20-45
Original file line numberDiff line numberDiff line change
@@ -193,36 +193,24 @@ function canPartition(nums: number[]): boolean {
193193

194194
```rust
195195
impl Solution {
196-
#[allow(dead_code)]
197196
pub fn can_partition(nums: Vec<i32>) -> bool {
198-
let mut sum = 0;
199-
for e in &nums {
200-
sum += *e;
201-
}
202-
203-
if sum % 2 != 0 {
197+
let s: i32 = nums.iter().sum();
198+
if s % 2 != 0 {
204199
return false;
205200
}
206-
201+
let m = (s / 2) as usize;
207202
let n = nums.len();
208-
let m = (sum / 2) as usize;
209-
let mut dp: Vec<Vec<bool>> = vec![vec![false; m + 1]; n + 1];
210-
211-
// Initialize the dp vector
212-
dp[0][0] = true;
213-
214-
// Begin the actual dp process
203+
let mut f = vec![vec![false; m + 1]; n + 1];
204+
f[0][0] = true;
205+
215206
for i in 1..=n {
207+
let x = nums[i - 1] as usize;
216208
for j in 0..=m {
217-
dp[i][j] = if (nums[i - 1] as usize) > j {
218-
dp[i - 1][j]
219-
} else {
220-
dp[i - 1][j] || dp[i - 1][j - (nums[i - 1] as usize)]
221-
};
209+
f[i][j] = f[i - 1][j] || (j >= x && f[i - 1][j - x]);
222210
}
223211
}
224-
225-
dp[n][m]
212+
213+
f[n][m]
226214
}
227215
}
228216
```
@@ -379,36 +367,23 @@ function canPartition(nums: number[]): boolean {
379367

380368
```rust
381369
impl Solution {
382-
#[allow(dead_code)]
383370
pub fn can_partition(nums: Vec<i32>) -> bool {
384-
let mut sum = 0;
385-
for e in &nums {
386-
sum += *e;
387-
}
388-
389-
if sum % 2 != 0 {
371+
let s: i32 = nums.iter().sum();
372+
if s % 2 != 0 {
390373
return false;
391374
}
375+
let m = (s / 2) as usize;
376+
let mut f = vec![false; m + 1];
377+
f[0] = true;
392378

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

411-
dp[m]
386+
f[m]
412387
}
413388
}
414389
```

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

+20-45
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
```
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
}
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)