Skip to content
Open
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
76 changes: 76 additions & 0 deletions CombinationSum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 0-1 recursion or choose/notChoose recursion
class Solution {

List<List<Integer>> result;

public List<List<Integer>> combinationSum(int[] candidates, int target) {
this.result = new ArrayList<>();
helper(candidates, target, new ArrayList<>(), 0);
return result;
}

private void helper (int[] candidates, int target, List<Integer> path, int index) {

// base
if (target == 0) {
result.add(new ArrayList<>(path)); // copy
return;
}

if (target < 0 || candidates.length == index) {
return;
}

// logic

// not choose
helper(candidates, target, path, index + 1);

// choose
path.add(candidates[index]);
helper(candidates, target - candidates[index], path, index);
path.remove(path.size() - 1);
}
}

// for loop based recursion
class Solution {

List<List<Integer>> result;

public List<List<Integer>> combinationSum(int[] candidates, int target) {
this.result = new ArrayList<>();
helper(candidates, target, new ArrayList<>(), 0);
return result;
}

private void helper (int[] candidates, int target, List<Integer> path, int pivot) {

// base
if (target == 0) {
result.add(new ArrayList<>(path)); // copy
return;
}

if (target < 0) {
return;
}

// logic
for (int i = pivot; i < candidates.length; i++) {

//action
path.add(candidates[i]);

//recurse
helper(candidates, target - candidates[i], path, i);

//backtrack
path.remove(path.size() - 1);
}

}
}

// Time = O(2^(M+N))
// Space = O(N)
80 changes: 80 additions & 0 deletions Subsets.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// 0-1 recursion
class Solution {

List<List<Integer>> result;

public List<List<Integer>> subsets(int[] nums) {
this.result = new ArrayList<>();
helper(nums, new ArrayList<>(), 0);
return result;
}

private void helper (int nums[], List<Integer> path, int index) {

// base
if (index == nums.length) {
result.add(new ArrayList<>(path)); // deep copy
return;
}

//not choose
helper(nums, path, index + 1);

//choose
path.add(nums[index]);
helper(nums, path, index + 1);

//backtrack
path.remove(path.size() - 1);
}
}

// for lopp recursion
class Solution {

List<List<Integer>> result;

public List<List<Integer>> subsets(int[] nums) {
this.result = new ArrayList<>();
helper(nums, new ArrayList<>(), 0);
return result;
}

private void helper (int nums[], List<Integer> path, int pivot) {

result.add(new ArrayList<>(path)); // deep copy

for(int i = pivot; i < nums.length; i++) {
path.add(nums[i]);
helper(nums, path, i + 1);
//backtrack
path.remove(path.size() - 1);
}
}
}

// Start with the empty subset in the result list.
//For each number, copy all current subsets and append the number to create new subsets.
class Solution {

public List<List<Integer>> subsets(int[] nums) {

List<List<Integer>> result = new ArrayList<>();
result.add(new ArrayList<>());

for(int i = 0; i < nums.length; i++) {
int size = result.size();
for(int j = 0; j < size; j++) {
List<Integer> temp = new ArrayList<>(result.get(j));
temp.add(nums[i]);
result.add(temp);
}
}

return result;
}
}


// Time = O(N*2^N)
// Space = O(N)