diff --git a/Problem1.java b/Problem1.java new file mode 100644 index 00000000..010a7f31 --- /dev/null +++ b/Problem1.java @@ -0,0 +1,83 @@ +// Time Complexity : O(m^2 * n^2) where m is the number of elements in candidates and n is the target +// Space Complexity : O(h) where h is the height of the tree +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : no + + +// Your code here along with comments explaining your approach + +/** + * Using iterative approach where we select a pivot and start the index from the pivot to get the combinations of all numbers. + * We are starting the iteration from the pivot because the number can be re-used again. + *

+ * Base case 1 is when target becomes less than 0. The target number cannot be achieved using the current path so we return. + * Base case 2 is when target becomes 0. The target number is achieved so we add the current path to the result. + */ +class IterativeSolution { + public List> combinationSum(int[] candidates, int target) { + List> result = new ArrayList<>(); + helper(candidates, 0, target, new ArrayList<>(), result); + return result; + } + + private void helper(int[] candidates, int pivot, int target, List path, List> result) { + //base + if (target < 0) + return; + if (target == 0) { + result.add(new ArrayList<>(path)); + return; + } + + for (int i = pivot; i < candidates.length; i++) { + path.add(candidates[i]); + helper(candidates, i, target - candidates[i], path, result); + // backtracking + path.remove(path.size() - 1); + } + + } +} + +// Time Complexity : O(m^2 * n^2) where m is the number of elements in candidates and n is the target +// Space Complexity : O(h) where h is the height of the tree +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : no + + +// Your code here along with comments explaining your approach + +/** + * Using recursive approach where we do the 0 and 1 case where we do not choose or choose the current element. + * When we select the element, we recursively try to find the target sum. Once that recursion is complete, we backtrack the current element from the recursion. + *

+ * Base case 1 is when the target becomes negative or we reach the end of all elements in the array. We return since target was not found + * Base case 2 is when the target becomes 0. We add the current path to the result. + */ +class RecursiveSolution { + public List> combinationSum(int[] candidates, int target) { + List> result = new ArrayList<>(); + helper(candidates, 0, target, new ArrayList<>(), result); + return result; + } + + private void helper(int[] candidates, int idx, int target, List path, List> result) { + //base + if (target < 0 || idx == candidates.length) + return; + if (target == 0) { + result.add(new ArrayList<>(path)); + return; + } + + // 0 + helper(candidates, idx + 1, target, path, result); + + // 1 + path.add(candidates[idx]); + helper(candidates, idx, target - candidates[idx], path, result); + + path.remove(path.size() - 1); + + } +} \ No newline at end of file diff --git a/Problem2.java b/Problem2.java new file mode 100644 index 00000000..45fbffdd --- /dev/null +++ b/Problem2.java @@ -0,0 +1,64 @@ +// Time Complexity : 4 ^ n where n is the length of nums string +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : no + + +// Your code here along with comments explaining your approach + +/** + * We create possible strings with pivot based recursion. At each level, we apply the operators and generate the current number which will be used by next level. + * When we reach the end of the num string, we check if the target value matches the current value and add it to the result. + */ +class Solution { + public List addOperators(String num, int target) { + List result = new ArrayList<>(); + helper(num, target, 0, 0l, 0l, new StringBuilder(), result); + return result; + } + + private void helper(String num, int target, int pivot, long calc, long tail, StringBuilder path, List result) { + //if all numbers are taken into consideration + if (pivot == num.length()) { + if (calc == target) { + result.add(path.toString()); + } + return; + } + + for (int i = pivot; i < num.length(); i++) { + // leading 0 needs to be ignored + if (num.charAt(pivot) == '0' && i != pivot) + return; + long curr = Long.parseLong(num.substring(pivot, i + 1)); + int le = path.length(); + // first level so the current and tail are the number itself + if (pivot == 0) { + path.append(curr); + helper(num, target, i + 1, curr, curr, path, result); + path.setLength(le); + } else { + + // Adding to path, recurse and backtrack for all 3 actions + // + + path.append("+"); + path.append(curr); + helper(num, target, i + 1, calc + curr, curr, path, result); + path.setLength(le); + + // - + path.append("-"); + path.append(curr); + helper(num, target, i + 1, calc - curr, -curr, path, result); + path.setLength(le); + + // * + path.append("*"); + path.append(curr); + helper(num, target, i + 1, calc - tail + tail * curr, tail * curr, path, result); + path.setLength(le); + } + + } + } +} \ No newline at end of file