File tree Expand file tree Collapse file tree 3 files changed +20
-0
lines changed
partition-equal-subset-sum Expand file tree Collapse file tree 3 files changed +20
-0
lines changed Original file line number Diff line number Diff line change
1
+ import canPartition from "../partition-equal-subset-sum/index.ts" ;
2
+
3
+ export default canPartition ;
Original file line number Diff line number Diff line change @@ -45,6 +45,10 @@ Step 2. Add the dependency
45
45
46
46
<summary >展开查看</summary >
47
47
48
+ https://leetcode.cn/problems/partition-equal-subset-sum/
49
+
50
+ https://leetcode.cn/problems/NUPfPr/
51
+
48
52
https://leetcode.cn/problems/number-of-matching-subsequences
49
53
50
54
https://leetcode.cn/problems/is-subsequence/
Original file line number Diff line number Diff line change
1
+ export default function canPartition ( nums : number [ ] ) : boolean {
2
+ const sum : number = nums . reduce ( ( pre , cur ) => pre + cur ) ;
3
+ if ( sum % 2 === 1 ) return false ;
4
+ const bagSize : number = sum / 2 ;
5
+ const goodsNum : number = nums . length ;
6
+ const dp : number [ ] = new Array ( bagSize + 1 ) . fill ( 0 ) ;
7
+ for ( let i = 0 ; i < goodsNum ; i ++ ) {
8
+ for ( let j = bagSize ; j >= nums [ i ] ; j -- ) {
9
+ dp [ j ] = Math . max ( dp [ j ] , dp [ j - nums [ i ] ] + nums [ i ] ) ;
10
+ }
11
+ }
12
+ return dp [ bagSize ] === bagSize ;
13
+ }
You can’t perform that action at this time.
0 commit comments