From 256327124fd4cd3bb2ea96b8463df12e39b3d79b Mon Sep 17 00:00:00 2001 From: sainathek Date: Thu, 26 Mar 2026 10:42:23 -0400 Subject: [PATCH] Done Backtracking-1 --- problem1.java | 51 +++++++++++++++++++++++++++++++ problem2.java | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 problem1.java create mode 100644 problem2.java diff --git a/problem1.java b/problem1.java new file mode 100644 index 00000000..121104b6 --- /dev/null +++ b/problem1.java @@ -0,0 +1,51 @@ + +/** +Time Complexity = Exponential, approximately O(2^N) +Explanation: +For each element, we either choose it or skip it. +This results in exploring all possible combinations recursively. + +Space Complexity = O(Target) +Explanation: +The recursion stack and the current path list can grow up to +target / smallest candidate value. + +Did this code successfully run on LeetCode : Yes + +Any problem you faced while coding this : +Initially struggled to decide when to move the index forward and +when to stay on the same index to allow reuse of elements. +Fixed it by using: +- idx + 1 for "not choose" +- same idx for "choose" +*/ + +import java.util.*; + +class Solution { + + 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 cases + if (idx == candidates.length || target < 0) { + return; + } + if (target == 0) { + result.add(new ArrayList<>(path)); + return; + } + // Not choose current element + helper(candidates, idx + 1, target, path, result); + + // Choose current element + path.add(candidates[idx]); + helper(candidates, idx, target - candidates[idx], path, result); + // Backtrack + path.remove(path.size() - 1); + } +} diff --git a/problem2.java b/problem2.java new file mode 100644 index 00000000..1a80ed63 --- /dev/null +++ b/problem2.java @@ -0,0 +1,83 @@ + /** + Time Complexity : Exponential, approximately O(4^N) + Explanation: + At each position, we try different partitions of the number + and for each partition we try three operators: + +, -, * + So the total number of recursive states grows exponentially. + + Space Complexity : O(N) + Explanation: + Recursion depth can go up to N and StringBuilder path also holds + up to N characters/operators along one recursive path. + + Did this code successfully run on LeetCode : Yes + + Any problem you faced while coding this : + Initially the difficult part was handling multiplication, + because multiplication has higher precedence than + and -. + Fixed it by keeping track of the previous operand using 'tail'. + So when '*' is used: + calc = calc - tail + tail * curr + Also had to carefully handle numbers with leading zeros + like "05", which are not valid. + */ + class Solution { + + + + List res; + + public List addOperators(String num, int target) { + + this.res = new LinkedList<>(); + helper(num, 0, 0L, 0L, new StringBuilder(), target); + return res; + } + + private void helper(String num, int pivot, long calc, long tail, StringBuilder path, int target) { + + // Base case + if (pivot == num.length()) { + if (calc == target) { + res.add(path.toString()); + } + return; + } + + // Try every possible next number + for (int i = pivot; i < num.length(); i++) { + + // Skip numbers with leading zero + if (num.charAt(pivot) == '0' && pivot != i) break; + + long curr = Long.parseLong(num.substring(pivot, i + 1)); + + if (pivot == 0) { + // First number, no operator before it + int pl = path.length(); + path.append(curr); + helper(num, i + 1, curr, curr, path, target); + path.setLength(pl); + } else { + // Addition + int pl = path.length(); + path.append("+").append(curr); + helper(num, i + 1, calc + curr, curr, path, target); + path.setLength(pl); + + // Subtraction + pl = path.length(); + path.append("-").append(curr); + helper(num, i + 1, calc - curr, -curr, path, target); + path.setLength(pl); + + // Multiplication + pl = path.length(); + path.append("*").append(curr); + helper(num, i + 1, calc - tail + tail * curr, tail * curr, path, target); + path.setLength(pl); + } + } + } + } \ No newline at end of file