Skip to content

Problem 39 #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ As I work through this list I figure I would make a GitHub repo with my solution
40. [Number of Islands #200](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/number-of-islands-200.md)
41. [Rotting Oranges #994](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/rotting-oranges-994.md)
42. [Search in Rotated Sorted Array #33](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/search-rotated-array-33.md)
43. Combination Sum #39
43. [Combination Sum #39](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/combination-sum-39.md)
44. Permutations #46
45. [Merge Intervals #56](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/merge-intervals-56.md)
46. Lowest Common Ancestor of a Binary Tree #236
Expand Down
92 changes: 92 additions & 0 deletions medium/combination-sum-39.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Combination Sum

Page on leetcode: https://leetcode.com/problems/combination-sum/

## Problem Statement

Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order.

The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different.

It is guaranteed that the number of unique combinations that sum up to target is less than 150 combinations for the given input.

### Constraints

- 1 <= candidates.length <= 30
- 1 <= candidates[i] <= 200
- All elements of candidates are distinct.
- 1 <= target <= 500

### Example

```
Input: candidates = [2,3,6,7], target = 7
Output: [[2,2,3],[7]]
Explanation:
2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times.
7 is a candidate, and 7 = 7.
These are the only two combinations.
```

## Solution

- can I use greedy?
- array not sorted
- iterate thru each element
- need a result array

### Pseudocode

1. Iterate thru array
2. while sum is less than target keep summing
3. iterate on second loop

### Initial Attempt

```javascript
const combinationSum = function (candidates, target) {
for (let i = 0; i < candidates.length; i++) {
let sum = 0;
const possible = [];
while (sum < target) {
sum += candidates[i];
possible.push(candidate[i]);
for (let j = i + 1; j < candidates.length; j++) {}
}
}
};
```

### Optimized Solution

The below approach uses DFS recursion and a state space tree. The time complexity is O(2<sup>t</sup>) and space complexity is O(length of longest combo array). A discussion about time and space complexity can be found here: https://leetcode.com/problems/combination-sum/discuss/1755084/Detailed-Time-and-Space-Complecity-analysisc++javabacktracking

You can see an explanation of this solution here: https://www.youtube.com/watch?v=GBKI9VSKdGg

```javascript
const combinationSum = function (candidates, target) {
const result = [];

function dfs(i, combo, total) {
if (total === target) {
// Found a combo that adds to target, add a copy to the result so that you don't overwrite it on subsequent recursive calls.
result.push([...combo]);
return;
} else if (total > target || i >= candidates.length) {
// Can't go any further down this path
return;
}

// Continue down left path which adds another occurrence of candidates[i]
combo.push(candidates[i]);
dfs(i, combo, total + candidates[i]);

// Continue down the right path with no more occurrences of candidates[i]
combo.pop();
dfs(i + 1, combo, total);
}

dfs(0, [], 0);
return result;
};
```