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
43 changes: 43 additions & 0 deletions CombinationSumForBacktrack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import java.util.*;

class Solution {

List<List<Integer>> res;

public List<List<Integer>> combinationSum(int[] candidates, int target) {

res = new ArrayList<>();

helper(candidates, target, new ArrayList<>(), 0);

return res;
}

private void helper(int[] candidates,
int target,
List<Integer> path,
int indx) {

if (target < 0) {
return;
}

if (target == 0) {
res.add(new ArrayList<>(path));
return;
}

for (int i = indx; i < candidates.length; i++) {

path.add(candidates[i]);

helper(candidates,
target - candidates[i],
path,
i);

// backtrack
path.remove(path.size() - 1);
}
}
}
56 changes: 56 additions & 0 deletions CombinationSumRecurseBacktrack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

// Time Complexity : O(2^n+m)
// Space Complexity : Recursion stack space
// 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
// this is similar to the coin change problem
// we need to find the combinations of the numbers which adds up to the target
// no need of permutations, only the combinations and add to the res and return it
// we can do normal deep copy, for loop, backtrack anything.
import java.util.*;

class Solution {

List<List<Integer>> res;

public List<List<Integer>> combinationSum(int[] candidates, int target) {

res = new ArrayList<>();

helper(candidates, target, new ArrayList<>(), 0);

return res;
}

private void helper(int[] candidates,
int target,
List<Integer> path,
int indx) {

if (target < 0 || indx == candidates.length) {
return;
}

if (target == 0) {
res.add(new ArrayList<>(path));
return;
}

// Skip current number
helper(candidates, target, path, indx + 1);

// Take current number
path.add(candidates[indx]);

helper(candidates,
target - candidates[indx],
path,
indx);

// Backtrack
path.remove(path.size() - 1);
}
}
110 changes: 110 additions & 0 deletions Evaluateexp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Time Complexity :O(4^N)
// Space Complexity : Recursion stack space
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : no , just that 2 level tree


// Your code here along with comments explaining your approach
// Do 2 level tree recursion . first only for the numbers
// next for the operators
// for each operator do a seperate recursion, when it comes to * and % maintain a tail
// Strin builder + backtracking to avoid new space everytime.
import java.util.*;

public class Evaluateexp {
List<String> res;

public List<String> addOperators(String num, int target) {
this.res = new ArrayList<>();

helper(num, 0, 0L, new StringBuilder(), 0L, target);

return res;
}

private void helper(String num, int pivot, long calc,
StringBuilder path, long tail, int target) {

if (pivot == num.length()) {
if (calc == target) {
res.add(path.toString());
}
return;
}

for (int i = pivot; i < num.length(); i++) {

// Don't allow numbers with leading zeros
if (num.charAt(pivot) == '0' && i != pivot) {
break;
}

long curr = Long.parseLong(
num.substring(pivot, i + 1)
);

int len = path.length();

if (pivot == 0) {
path.append(curr);

helper(
num,
i + 1,
curr,
path,
curr,
target
);

path.setLength(len);
} else {

// +
path.append("+");
path.append(curr);

helper(
num,
i + 1,
calc + curr,
path,
curr,
target
);

path.setLength(len);

// -
path.append("-");
path.append(curr);

helper(
num,
i + 1,
calc - curr,
path,
-curr,
target
);

path.setLength(len);

// *
path.append("*");
path.append(curr);

helper(
num,
i + 1,
calc - tail + (tail * curr),
path,
tail * curr,
target
);

path.setLength(len);
}
}
}
}