-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsolution.ts
More file actions
32 lines (28 loc) · 783 Bytes
/
solution.ts
File metadata and controls
32 lines (28 loc) · 783 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
* @lc app=leetcode id=494 lang=javascript
*
* [494] Target Sum
*/
// @lc code=start
/**
* @param {number[]} nums
* @param {number} S
* @return {number}
*/
const findTargetSumWays = (nums: number[], S: number): number => {
// * dp ['188 ms', '71.81 %', '39.5 MB', '100 %']
// * https://leetcode-cn.com/problems/target-sum/solution/mu-biao-he-by-leetcode/
let pool = new Map<number, number>();
pool.set(0, 1);
nums.forEach((n, i) => {
const nextPool = new Map<number, number>();
pool.forEach((step, sum) => {
nextPool.set(sum + n, (nextPool.get(sum + n) || 0) + step);
nextPool.set(sum - n, (nextPool.get(sum - n) || 0) + step);
});
pool = nextPool;
});
return pool.get(S) || 0;
};
// @lc code=end
export { findTargetSumWays };