diff --git a/CombinationSum.java b/CombinationSum.java new file mode 100644 index 00000000..c08abca4 --- /dev/null +++ b/CombinationSum.java @@ -0,0 +1,39 @@ +import java.util.ArrayList; +import java.util.List; + +/* + * Combination Sum - LeetCode 39 + * Approach: Backtracking. Try all combinations recursively by including or excluding each candidate. + * Time Complexity: O(2^n * k), where n is the number of candidates and k is the average length of a combination. + * Space Complexity: O(k * x), where x is the number of valid combinations. + * LeetCode Link: https://leetcode.com/problems/combination-sum/ + */ + +public class CombinationSum { + + 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) { + + if (idx == candidates.length || target < 0) return; + //logic + if( target == 0) { + result.add(new ArrayList<>(path)); + return; + } + + helper(candidates, target, idx+1, path, result); + + //action + path.add(candidates[idx]); + //recurse + helper(candidates, target-candidates[idx], idx, path, result); + //backtrack + path.removeLast(); + + } +} diff --git a/CombinationSum2.java b/CombinationSum2.java new file mode 100644 index 00000000..d46e95e7 --- /dev/null +++ b/CombinationSum2.java @@ -0,0 +1,38 @@ +import java.util.ArrayList; +import java.util.List; + +/* + * Combination Sum (Variation) - LeetCode 39 + * Approach: Backtracking. Try all combinations recursively, allowing repeated use of elements. + * Time Complexity: O(2^n * k), where n is the number of candidates and k is the average length of a combination. + * Space Complexity: O(k * x), where x is the number of valid combinations. + * LeetCode Link: https://leetcode.com/problems/combination-sum/ + */ + +public class CombinationSum2 { + + 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) { + if (target < 0) return; + + if (target == 0) { + result.add(new ArrayList<>(path)); + return; + } + + //base + for (int i = pivot; i < candidates.length; i++) { + //action + path.add(candidates[i]); + //recurse + helper(candidates, i, target - candidates[i], path, result); + //backtrack + path.remove(path.size() - 1); + } + } +} diff --git a/ExpressionAddOperators.java b/ExpressionAddOperators.java new file mode 100644 index 00000000..f03429e7 --- /dev/null +++ b/ExpressionAddOperators.java @@ -0,0 +1,54 @@ +/* + * Expression Add Operators - LeetCode 282 + * Approach: Backtracking. Recursively try all possible operator insertions between digits. + * Time Complexity: O(4^n), where n is the length of the input string. + * Space Complexity: O(n), for recursion stack and path building. + * LeetCode Link: https://leetcode.com/problems/expression-add-operators/ + */ + +import java.util.ArrayList; +import java.util.List; + +public class ExpressionAddOperators { + List result; + + public List addOperators(String num, int target) { + result = new ArrayList<>(); + helper(num, 0, 0, 0, "", target); + return result; + } + + private void helper(String num, int pivot, long currentValue, long tale, String path, int target) { + //base + if (pivot == num.length()) { + if (currentValue == target) { + result.add(path); + } + return; + } + + for (int i = pivot; i < num.length(); i++) { + if (i != pivot && num.charAt(pivot) == '0') continue; + long current = Long.parseLong(num.substring(pivot, i + 1)); + if (pivot == 0) { + helper(num, i + 1, current, current, path + current, target); + } else { + // + operation + helper(num, i + 1, currentValue + current, current, path + '+' + current, target); + + // - operation + helper(num, i + 1, currentValue - current, -current, path + '-' + current, target); + + // * operation + helper(num, i + 1, currentValue - tale + current * tale, tale * current, path + '*' + current, target); + } + + } + + } + + + public static void main(String[] args) { + System.out.println(new ExpressionAddOperators().addOperators("123", 9)); + } +} diff --git a/ExpressionAddOperators2.java b/ExpressionAddOperators2.java new file mode 100644 index 00000000..f2b47e3e --- /dev/null +++ b/ExpressionAddOperators2.java @@ -0,0 +1,66 @@ +/* + * Expression Add Operators - LeetCode 282 + * Approach: Backtracking. Recursively try all possible operator insertions between digits. Uses StringBuilder for efficient path building. + * Time Complexity: O(4^n), where n is the length of the input string. + * Space Complexity: O(n), for recursion stack and path building. + * LeetCode Link: https://leetcode.com/problems/expression-add-operators/ + */ + +import java.util.ArrayList; +import java.util.List; + +public class ExpressionAddOperators2 { + List result; + + public List addOperators(String num, int target) { + result = new ArrayList<>(); + helper(num, 0, 0, 0, new StringBuilder(), target); + return result; + } + + private void helper(String num, int pivot, long currentValue, long tale, StringBuilder path, int target) { + //base + if (pivot == num.length()) { + if (currentValue == target) { + result.add(path.toString()); + } + return; + } + + for (int i = pivot; i < num.length(); i++) { + if (i != pivot && num.charAt(pivot) == '0') continue; + long current = Long.parseLong(num.substring(pivot, i + 1)); + int length = path.length(); + if (pivot == 0) { + path.append(current); + helper(num, i + 1, current, current, path, target); + path.setLength(length); + } else { + // + operation + path.append("+"); + path.append(current); + helper(num, i + 1, currentValue + current, current, path, target); + path.setLength(length); + + // - operation + path.append("-"); + path.append(current); + helper(num, i + 1, currentValue - current, -current, path, target); + path.setLength(length); + + // * operation + path.append("*"); + path.append(current); + helper(num, i + 1, currentValue - tale + current * tale, tale * current, path, target); + path.setLength(length); + } + + } + + } + + + public static void main(String[] args) { + System.out.println(new ExpressionAddOperators2().addOperators("123", 9)); + } +}