diff --git a/Problem1.java b/Problem1.java new file mode 100644 index 00000000..50d57740 --- /dev/null +++ b/Problem1.java @@ -0,0 +1,32 @@ +// https://leetcode.com/problems/combination-sum/ +// Time Complexity : O(2^(m+n)) where m is the number of candidates and n is the target value. +// Space Complexity : O(n) where n is the maximum depth of the recursion tree. +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + + +class Solution { + public List> combinationSum(int[] candidates, int target) { + List> result = new ArrayList<>(); + helper(candidates, target, 0, new ArrayList<>(), result); + return result; + } + private void helper(int[] candidates, int target, int idx, List path, List> result){ + //base case + if(idx == candidates.length || target < 0) return; + if(target == 0){ + result.add(new ArrayList(path)); + return; + } + // logic + // no choose + helper(candidates, target, idx+1, path, result); + //choose + // action + path.add(candidates[idx]); + // recurse + helper(candidates, target-candidates[idx], idx, path, result); + // backtrack + 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..53938e2a --- /dev/null +++ b/Problem2.java @@ -0,0 +1,70 @@ +// https://leetcode.com/problems/expression-add-operators/ +// Time Complexity : O(4^n) where n is the length of the input string num. +// This is because for each digit, we have 4 choices: add an operator (+, -, *, or no operator). +// Space Complexity : O(n) where n is the length of the input string num. +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +class Solution { + public List addOperators(String num, int target) { + List result = new ArrayList<>(); + helper(num, 0, 0l, 0l, new StringBuilder(), target, result); + return result; + } + + private void helper(String num, int pivot, long calc, long tail, StringBuilder path, int target, + List result) { + //base + if (pivot == num.length()) { + if (calc == target) { + result.add(path.toString()); + } + return; + } + //logic + for (int i = pivot; i < num.length(); i++) { + long curr = Long.parseLong(num.substring(pivot, i + 1)); + if (num.charAt(pivot) == '0' && pivot != i) { + continue; + } + int le = path.length(); + if (pivot == 0) { + //top level + // 1 12 123 + // action + path.append(curr); + // recurse + helper(num, i + 1, curr, curr, path, target, result); + // backtrack + path.setLength(le); + } else { + // + + // action + path.append("+"); + path.append(curr); + // recurse + helper(num, i + 1, calc + curr, curr, path, target, result); + // backtrack + path.setLength(le); + + // - + // action + path.append("-"); + path.append(curr); + // recurse + helper(num, i + 1, calc - curr, -curr, path, target, result); + // backtrack + path.setLength(le); + + // * + // action + path.append("*"); + path.append(curr); + // recurse + helper(num, i + 1, calc - tail + tail * curr, tail * curr, path, target, result); + // backtrack + path.setLength(le); + } + } + } +} \ No newline at end of file