Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Problem-1.java
Original file line number Diff line number Diff line change
@@ -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<List<Integer>> result;
public List<List<Integer>> 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<Integer> 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);
}
}
}
52 changes: 52 additions & 0 deletions Problem-1.py
Original file line number Diff line number Diff line change
@@ -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<List<Integer>> result;
public List<List<Integer>> 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<Integer> 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()


56 changes: 56 additions & 0 deletions Problem-2.java
Original file line number Diff line number Diff line change
@@ -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<String> result;
public List<String> 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);

}
}
}
}
36 changes: 36 additions & 0 deletions Problem-2.py
Original file line number Diff line number Diff line change
@@ -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)