diff --git a/Problem-1.java b/Problem-1.java new file mode 100644 index 00000000..4b05934c --- /dev/null +++ b/Problem-1.java @@ -0,0 +1,35 @@ + +// Time Complexity : O(2^(m+n)) +// 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 +// 1. Use DFS/backtracking to explore all combinations of candidates that sum up to the target. +// 2. Track the current combination (path) and remaining target, allowing repeated use of each candidate. +// 3. Add a copy of the path to the result when the remaining target becomes 0, and backtrack to explore other options. + +class Solution { + List> result; + public List> combinationSum(int[] candidates, int target) { + this.result = new ArrayList<>(); + helper(candidates, target, 0, new ArrayList<>()); + return result; + } + public void helper(int[] candidates, int target, int pivot, List path){ + 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,target-candidates[i], i, path); + path.remove(path.size()-1); + } + } +} \ No newline at end of file diff --git a/Problem-1.py b/Problem-1.py new file mode 100644 index 00000000..d68d815a --- /dev/null +++ b/Problem-1.py @@ -0,0 +1,52 @@ + + +# Time Complexity : O(2^(m+n)) +# 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 +# 1. Use DFS/backtracking to explore all combinations of candidates that sum up to the target. +# 2. Track the current combination (path) and remaining target, allowing repeated use of each candidate. +# 3. Add a copy of the path to the result when the remaining target becomes 0, and backtrack to explore other options. + +class Solution { + List> result; + public List> combinationSum(int[] candidates, int target) { + this.result = new ArrayList<>(); + helper(candidates, target, 0, new ArrayList<>()); + return result; + } + public void helper(int[] candidates, int target, int pivot, List path){ + 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,target-candidates[i], i, path); + path.remove(path.size()-1); + } + } +}class Solution: + def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: + self.result = [] + self.helper(candidates, target,[],0) + return self.result + def helper(self,candidates,target, path, pivot): + if target <0: + return + if target == 0: + self.result.append(list(path)) + return + for i in range(pivot, len(candidates)): + path.append(candidates[i]) + self.helper(candidates,target-candidates[i], path, i) + path.pop() + + \ No newline at end of file diff --git a/Problem-2.java b/Problem-2.java new file mode 100644 index 00000000..56d1d364 --- /dev/null +++ b/Problem-2.java @@ -0,0 +1,56 @@ +// Time Complexity : O(4^n) +// 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 +// 1. Use DFS/backtracking to try all ways of inserting '+', '-', and '*' between digits. +// 2. Track the current calculation (cal) and last operand (tail) to correctly handle multiplication precedence. +// 3. Skip numbers with leading zeros and add the expression to the result when the end is reached and cal == target. + +class Solution { + List result; + public List addOperators(String num, int target) { + this.result = new ArrayList<>(); + helper(num, target, 0, 0, new StringBuilder(), 0); + return result; + } + + public void helper(String num, int target, long cal, long tail, StringBuilder path, int pivot){ + if(pivot == num.length()){ + if (cal == target){ + result.add(path.toString()); + return; + } + } + Long curr; + + for (int i = pivot; i < num.length(); i++){ + if (num.charAt(pivot) == '0' && pivot != i){ + break; + } + + curr = Long.parseLong(num.substring(pivot,i+1)); + int le = path.length(); + + if (pivot == 0){ + path.append(curr); + helper(num,target,curr,curr,path,i+1); + path.setLength(le); + } + else{ + path.append("+").append(curr); + helper(num,target,cal+curr,curr, path,i+1 ); + path.setLength(le); + path.append("-").append(curr); + helper(num,target,cal-curr,-curr, path,i+1 ); + path.setLength(le); + path.append("*").append(curr); + helper(num,target,cal-tail+tail*curr,tail*curr, path,i+1 ); + path.setLength(le); + + } + } + } +} \ No newline at end of file diff --git a/Problem-2.py b/Problem-2.py new file mode 100644 index 00000000..72c93253 --- /dev/null +++ b/Problem-2.py @@ -0,0 +1,36 @@ +# Time Complexity : O(4^n) +# 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 +# 1. Use DFS/backtracking to try all ways of inserting '+', '-', and '*' between digits. +# 2. Track the current calculation (cal) and last operand (tail) to correctly handle multiplication precedence. +# 3. Skip numbers with leading zeros and add the expression to the result when the end is reached and cal == target. + +class Solution: + def addOperators(self, num: str, target: int) -> List[str]: + self.result = [] + self.helper(num,target, 0, 0,"",0) + return self.result + def helper(self, num, target, cal, tail, path,pivot): + if pivot == len(num): + if cal == target: + self.result.append(path) + return + + for i in range(pivot,len(num)): + + if num[pivot] == '0' and pivot != i: + break + + curr = int(num[pivot:i+1]) + if pivot == 0: + self.helper(num,target,curr,curr,path + str(curr),i+1) + else: + self.helper(num, target,cal+curr, curr, path + "+" + str(curr), i+1) + self.helper(num, target, cal-curr, -curr, path + "-" + str(curr), i+1) + self.helper(num, target, cal-tail + tail*curr,tail*curr , path + "*" + str(curr), i+1) + +