Skip to content

BackTracking-1 #1035

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
30 changes: 30 additions & 0 deletions CombinationSum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Time complexity - O(2^n)
// Space Complexity - O(n)
// Solved on leetcode - yes
// did you face any issues - No
// The algorithm uses backtracking to explore all possible combinations of candidates that sum up to the target. At each step, it either chooses the current element (allowing repeated use by staying at the same index) or skips it (by moving to the next index). When the target becomes zero, the current path is added to the result list, and the function backtracks to explore other possibilities.
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 index, List<Integer> path) {
//base
if(target<0 || index == candidates.length) return;
if(target == 0) {
result.add(new ArrayList<>(path));
return;
}
//logic
//not choose
helper(candidates, target, index+1, path);
// choose
path.add(candidates[index]);
helper(candidates, target-candidates[index], index, path);
path.remove(path.size()-1);

}
}
43 changes: 43 additions & 0 deletions ExpressionAdd.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Time complexity - O(n)
// Space complexity - O(n)
// DID you solve on leetcode - yes
// Did you face any issues - no
// The algorithm uses depth-first search (DFS) with backtracking to explore all ways of inserting +, -, and * operators between digits of the string to form valid arithmetic expressions. At each recursive step, it builds a substring, converts it to a number, and appends it to the current expression along with an operator while maintaining the running total and last operand (to handle multiplication precedence). It adds the expression to the result list only if the entire string is consumed and the evaluated value matches the target.
class Solution {
List<String> result = new ArrayList<>();

public List<String> addOperators(String num, int target) {
helper(num, target, 0, 0, 0, "");
return result;
}

private void helper(String num, int target, int index, long currentValue, long lastOperand, String expression) {
if (index == num.length()) {
if (currentValue == target) {
result.add(expression);
}
return;
}

for (int i = index; i < num.length(); i++) {
// Skip numbers with leading zero
if (i != index && num.charAt(index) == '0') break;

String currentStr = num.substring(index, i + 1);
System.out.println(currentStr);
long currentNum = Long.parseLong(currentStr);

if (index == 0) {
// First number, no operator before it
helper(num, target, i + 1, currentNum, currentNum, currentStr);
} else {
helper(num, target, i + 1, currentValue + currentNum, currentNum, expression + "+" + currentStr);
helper(num, target, i + 1, currentValue - currentNum, -currentNum, expression + "-" + currentStr);
helper(num, target, i + 1,
currentValue - lastOperand + lastOperand * currentNum,
lastOperand * currentNum,
expression + "*" + currentStr);
}
}
}
}