Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e186a32

Browse files
committed
Add solution #377
1 parent 121c48b commit e186a32

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

0377-combination-sum-iv.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* 377. Combination Sum IV
3+
* https://leetcode.com/problems/combination-sum-iv/
4+
* Difficulty: Medium
5+
*
6+
* Given an array of distinct integers nums and a target integer target, return the number of
7+
* possible combinations that add up to target.
8+
*
9+
* The test cases are generated so that the answer can fit in a 32-bit integer.
10+
*/
11+
12+
/**
13+
* @param {number[]} nums
14+
* @param {number} target
15+
* @return {number}
16+
*/
17+
var combinationSum4 = function(nums, target) {
18+
const dp = new Array(target + 1).fill(0);
19+
dp[0] = 1;
20+
21+
for (let i = 1; i <= target; i++) {
22+
nums.forEach(n => dp[i] += i >= n ? dp[i - n] : 0);
23+
}
24+
25+
return dp[target];
26+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@
296296
374|[Guess Number Higher or Lower](./0374-guess-number-higher-or-lower.js)|Medium|
297297
375|[Guess Number Higher or Lower II](./0375-guess-number-higher-or-lower-ii.js)|Medium|
298298
376|[Wiggle Subsequence](./0376-wiggle-subsequence.js)|Medium|
299+
377|[Combination Sum IV](./0377-combination-sum-iv.js)|Medium|
299300
378|[Kth Smallest Element in a Sorted Matrix](./0378-kth-smallest-element-in-a-sorted-matrix.js)|Medium|
300301
380|[Insert Delete GetRandom O(1)](./0380-insert-delete-getrandom-o1.js)|Medium|
301302
383|[Ransom Note](./0383-ransom-note.js)|Easy|

0 commit comments

Comments
 (0)