diff --git a/CombinationSum.java b/CombinationSum.java new file mode 100644 index 00000000..e55fe4ed --- /dev/null +++ b/CombinationSum.java @@ -0,0 +1,76 @@ +// 0-1 recursion or choose/notChoose recursion +class Solution { + + List> result; + + public List> 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 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> result; + + public List> 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 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) \ No newline at end of file diff --git a/Subsets.java b/Subsets.java new file mode 100644 index 00000000..493d0a74 --- /dev/null +++ b/Subsets.java @@ -0,0 +1,80 @@ +// 0-1 recursion +class Solution { + + List> result; + + public List> subsets(int[] nums) { + this.result = new ArrayList<>(); + helper(nums, new ArrayList<>(), 0); + return result; + } + + private void helper (int nums[], List 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> result; + + public List> subsets(int[] nums) { + this.result = new ArrayList<>(); + helper(nums, new ArrayList<>(), 0); + return result; + } + + private void helper (int nums[], List 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> subsets(int[] nums) { + + List> 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 temp = new ArrayList<>(result.get(j)); + temp.add(nums[i]); + result.add(temp); + } + } + + return result; + } +} + + +// Time = O(N*2^N) +// Space = O(N) \ No newline at end of file