File tree Expand file tree Collapse file tree 17 files changed +554
-450
lines changed
solution/0400-0499/0494.Target Sum Expand file tree Collapse file tree 17 files changed +554
-450
lines changed Load Diff Large diffs are not rendered by default.
Load Diff Large diffs are not rendered by default.
Original file line number Diff line number Diff line change @@ -2,16 +2,22 @@ class Solution {
2
2
public:
3
3
int findTargetSumWays (vector<int >& nums, int target) {
4
4
int s = accumulate (nums.begin (), nums.end (), 0 );
5
- if (s < target || (s - target) % 2 != 0 ) return 0 ;
6
- int m = nums.size (), n = (s - target) / 2 ;
7
- vector<vector<int >> dp (m + 1 , vector<int >(n + 1 ));
8
- dp[0 ][0 ] = 1 ;
5
+ if (s < target || (s - target) % 2 ) {
6
+ return 0 ;
7
+ }
8
+ int m = nums.size ();
9
+ int n = (s - target) / 2 ;
10
+ int f[m + 1 ][n + 1 ];
11
+ memset (f, 0 , sizeof (f));
12
+ f[0 ][0 ] = 1 ;
9
13
for (int i = 1 ; i <= m; ++i) {
10
14
for (int j = 0 ; j <= n; ++j) {
11
- dp[i][j] += dp[i - 1 ][j];
12
- if (nums[i - 1 ] <= j) dp[i][j] += dp[i - 1 ][j - nums[i - 1 ]];
15
+ f[i][j] = f[i - 1 ][j];
16
+ if (j >= nums[i - 1 ]) {
17
+ f[i][j] += f[i - 1 ][j - nums[i - 1 ]];
18
+ }
13
19
}
14
20
}
15
- return dp [m][n];
21
+ return f [m][n];
16
22
}
17
23
};
Original file line number Diff line number Diff line change 1
1
func findTargetSumWays (nums []int , target int ) int {
2
2
s := 0
3
- for _ , v := range nums {
4
- s += v
3
+ for _ , x := range nums {
4
+ s += x
5
5
}
6
6
if s < target || (s - target )% 2 != 0 {
7
7
return 0
8
8
}
9
9
m , n := len (nums ), (s - target )/ 2
10
- dp := make ([][]int , m + 1 )
11
- for i := range dp {
12
- dp [i ] = make ([]int , n + 1 )
10
+ f := make ([][]int , m + 1 )
11
+ for i := range f {
12
+ f [i ] = make ([]int , n + 1 )
13
13
}
14
- dp [0 ][0 ] = 1
14
+ f [0 ][0 ] = 1
15
15
for i := 1 ; i <= m ; i ++ {
16
16
for j := 0 ; j <= n ; j ++ {
17
- dp [i ][j ] = dp [i - 1 ][j ]
18
- if nums [i - 1 ] <= j {
19
- dp [i ][j ] += dp [i - 1 ][j - nums [i - 1 ]]
17
+ f [i ][j ] = f [i - 1 ][j ]
18
+ if j >= nums [i - 1 ] {
19
+ f [i ][j ] += f [i - 1 ][j - nums [i - 1 ]]
20
20
}
21
21
}
22
22
}
23
- return dp [m ][n ]
23
+ return f [m ][n ]
24
24
}
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public int findTargetSumWays (int [] nums , int target ) {
3
- int s = 0 ;
4
- for (int v : nums ) {
5
- s += v ;
6
- }
3
+ int s = Arrays .stream (nums ).sum ();
7
4
if (s < target || (s - target ) % 2 != 0 ) {
8
5
return 0 ;
9
6
}
10
7
int m = nums .length ;
11
8
int n = (s - target ) / 2 ;
12
- int [][] dp = new int [m + 1 ][n + 1 ];
13
- dp [0 ][0 ] = 1 ;
9
+ int [][] f = new int [m + 1 ][n + 1 ];
10
+ f [0 ][0 ] = 1 ;
14
11
for (int i = 1 ; i <= m ; ++i ) {
15
12
for (int j = 0 ; j <= n ; ++j ) {
16
- dp [i ][j ] = dp [i - 1 ][j ];
17
- if (nums [i - 1 ] <= j ) {
18
- dp [i ][j ] += dp [i - 1 ][j - nums [i - 1 ]];
13
+ f [i ][j ] = f [i - 1 ][j ];
14
+ if (j >= nums [i - 1 ]) {
15
+ f [i ][j ] += f [i - 1 ][j - nums [i - 1 ]];
19
16
}
20
17
}
21
18
}
22
- return dp [m ][n ];
19
+ return f [m ][n ];
23
20
}
24
21
}
Original file line number Diff line number Diff line change 4
4
* @return {number }
5
5
*/
6
6
var findTargetSumWays = function ( nums , target ) {
7
- let s = 0 ;
8
- for ( let v of nums ) {
9
- s += v ;
10
- }
11
- if ( s < target || ( s - target ) % 2 != 0 ) {
7
+ const s = nums . reduce ( ( a , b ) => a + b , 0 ) ;
8
+ if ( s < target || ( s - target ) % 2 ) {
12
9
return 0 ;
13
10
}
14
- const m = nums . length ;
15
- const n = ( s - target ) / 2 ;
16
- let dp = new Array ( n + 1 ) . fill ( 0 ) ;
17
- dp [ 0 ] = 1 ;
18
- for ( let i = 1 ; i <= m ; ++ i ) {
19
- for ( let j = n ; j >= nums [ i - 1 ] ; -- j ) {
20
- dp [ j ] += dp [ j - nums [ i - 1 ] ] ;
11
+ const [ m , n ] = [ nums . length , ( ( s - target ) / 2 ) | 0 ] ;
12
+ const f = Array . from ( { length : m + 1 } , ( ) => Array ( n + 1 ) . fill ( 0 ) ) ;
13
+ f [ 0 ] [ 0 ] = 1 ;
14
+ for ( let i = 1 ; i <= m ; i ++ ) {
15
+ for ( let j = 0 ; j <= n ; j ++ ) {
16
+ f [ i ] [ j ] = f [ i - 1 ] [ j ] ;
17
+ if ( j >= nums [ i - 1 ] ) {
18
+ f [ i ] [ j ] += f [ i - 1 ] [ j - nums [ i - 1 ] ] ;
19
+ }
21
20
}
22
21
}
23
- return dp [ n ] ;
22
+ return f [ m ] [ n ] ;
24
23
} ;
Original file line number Diff line number Diff line change 1
1
class Solution :
2
2
def findTargetSumWays (self , nums : List [int ], target : int ) -> int :
3
3
s = sum (nums )
4
- if s < target or (s - target ) % 2 != 0 :
4
+ if s < target or (s - target ) % 2 :
5
5
return 0
6
6
m , n = len (nums ), (s - target ) // 2
7
- dp = [[0 ] * (n + 1 ) for _ in range (m + 1 )]
8
- dp [0 ][0 ] = 1
9
- for i in range ( 1 , m + 1 ):
7
+ f = [[0 ] * (n + 1 ) for _ in range (m + 1 )]
8
+ f [0 ][0 ] = 1
9
+ for i , x in enumerate ( nums , 1 ):
10
10
for j in range (n + 1 ):
11
- dp [i ][j ] = dp [i - 1 ][j ]
12
- if nums [ i - 1 ] <= j :
13
- dp [i ][j ] += dp [i - 1 ][j - nums [ i - 1 ] ]
14
- return dp [ - 1 ][ - 1 ]
11
+ f [i ][j ] = f [i - 1 ][j ]
12
+ if j >= x :
13
+ f [i ][j ] += f [i - 1 ][j - x ]
14
+ return f [ m ][ n ]
Original file line number Diff line number Diff line change 1
1
impl Solution {
2
- #[ allow( dead_code) ]
3
2
pub fn find_target_sum_ways ( nums : Vec < i32 > , target : i32 ) -> i32 {
4
- let mut sum = 0 ;
5
- for e in & nums {
6
- sum += * e;
7
- }
8
-
9
- // -x + (sum - x) = target <-> -2 * x + sum = target <-> 2 * x = sum - target
10
- if sum < target || ( sum - target) % 2 != 0 {
11
- // There is no way to get any expression in this case
3
+ let s: i32 = nums. iter ( ) . sum ( ) ;
4
+ if s < target || ( s - target) % 2 != 0 {
12
5
return 0 ;
13
6
}
14
- let n = nums. len ( ) ;
15
- let m = ( sum - target) / 2 ;
16
-
17
- let mut dp: Vec < Vec < i32 > > = vec ! [ vec![ 0 ; m as usize + 1 ] ; n + 1 ] ;
18
-
19
- // Initialize the dp vector
20
- dp[ 0 ] [ 0 ] = 1 ;
21
-
22
- // Begin the actual dp phase
23
- for i in 1 ..=n {
24
- for j in 0 ..=m as usize {
25
- // nums[i - 1] is not included
26
- dp[ i] [ j] = dp[ i - 1 ] [ j] ;
27
- if nums[ i - 1 ] <= ( j as i32 ) {
28
- // nums[i - 1] is included
29
- dp[ i] [ j] += dp[ i - 1 ] [ j - ( nums[ i - 1 ] as usize ) ] ;
7
+ let m = nums. len ( ) ;
8
+ let n = ( ( s - target) / 2 ) as usize ;
9
+ let mut f = vec ! [ vec![ 0 ; n + 1 ] ; m + 1 ] ;
10
+ f[ 0 ] [ 0 ] = 1 ;
11
+ for i in 1 ..=m {
12
+ for j in 0 ..=n {
13
+ f[ i] [ j] = f[ i - 1 ] [ j] ;
14
+ if j as i32 >= nums[ i - 1 ] {
15
+ f[ i] [ j] += f[ i - 1 ] [ j - nums[ i - 1 ] as usize ] ;
30
16
}
31
17
}
32
18
}
33
-
34
- dp[ n] [ m as usize ]
19
+ f[ m] [ n]
35
20
}
36
21
}
Original file line number Diff line number Diff line change
1
+ function findTargetSumWays ( nums : number [ ] , target : number ) : number {
2
+ const s = nums . reduce ( ( a , b ) => a + b , 0 ) ;
3
+ if ( s < target || ( s - target ) % 2 ) {
4
+ return 0 ;
5
+ }
6
+ const [ m , n ] = [ nums . length , ( ( s - target ) / 2 ) | 0 ] ;
7
+ const f : number [ ] [ ] = Array . from ( { length : m + 1 } , ( ) => Array ( n + 1 ) . fill ( 0 ) ) ;
8
+ f [ 0 ] [ 0 ] = 1 ;
9
+ for ( let i = 1 ; i <= m ; i ++ ) {
10
+ for ( let j = 0 ; j <= n ; j ++ ) {
11
+ f [ i ] [ j ] = f [ i - 1 ] [ j ] ;
12
+ if ( j >= nums [ i - 1 ] ) {
13
+ f [ i ] [ j ] += f [ i - 1 ] [ j - nums [ i - 1 ] ] ;
14
+ }
15
+ }
16
+ }
17
+ return f [ m ] [ n ] ;
18
+ }
Original file line number Diff line number Diff line change @@ -2,13 +2,18 @@ class Solution {
2
2
public:
3
3
int findTargetSumWays (vector<int >& nums, int target) {
4
4
int s = accumulate (nums.begin (), nums.end (), 0 );
5
- if (s < target || (s - target) % 2 != 0 ) return 0 ;
5
+ if (s < target || (s - target) % 2 ) {
6
+ return 0 ;
7
+ }
6
8
int n = (s - target) / 2 ;
7
- vector<int > dp (n + 1 );
8
- dp[0 ] = 1 ;
9
- for (int & v : nums)
10
- for (int j = n; j >= v; --j)
11
- dp[j] += dp[j - v];
12
- return dp[n];
9
+ int f[n + 1 ];
10
+ memset (f, 0 , sizeof (f));
11
+ f[0 ] = 1 ;
12
+ for (int x : nums) {
13
+ for (int j = n; j >= x; --j) {
14
+ f[j] += f[j - x];
15
+ }
16
+ }
17
+ return f[n];
13
18
}
14
19
};
You can’t perform that action at this time.
0 commit comments