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
39 changes: 39 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Time Complexity : O(2 ^ (m+n)) where m is the candidates length and n is the target
// 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
//Using for loop based recursion we apprach the problem
//At each loop we choose the element and update the target and backtrack the path
//once the target becomes zero we add the path to the res
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
helper(candidates, 0, path, target, res);
return res;

}

private void helper(int[] candidates, int pivot, List<Integer> path, int target, List<List<Integer>> res) {
//base
if (target < 0) return;
if (target == 0) {
//found a path
res.add(new ArrayList<>(path));
return;
}
//logic
for(int i = pivot; i < candidates.length; i++) {
//action
path.add(candidates[i]);
//recurse
helper(candidates, i, path, target-candidates[i], res);
//backtrack
path.remove(path.size() - 1);
}

}
}
54 changes: 54 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 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
//Using recusion we first get the substrings of the the given num string
//we either have a case with operators or without operators
//with operators we further recursively caluclate the expression and evualthe res which will be calc value and curr will be the number being evaluated
//Tail value is being stored which would be the change that we apply at each call
//For +, calculate val = calc + cur and tail val = +cur
//For -, calculate val = calc - cur and tail val = -cur
//For *, to adhere to BODMAS rule, calculate val = calc - tail + (tail * cur) and tail val = (tail * cur)

class Solution {
public List<String> addOperators(String num, int target) {
List<String> res = new ArrayList<>();
helper(num, 0, 0l, 0l, "", target, res);
return res;
}

private void helper(String num, int pivot, long calc, long tail,String path, int target, List<String> res) {
//base
if(pivot == num.length()) {
if(target == calc) {
res.add(path);
return;
}
}

//logic
//form the numbers
for(int i = pivot; i < num.length(); i++) {
//preceeding 0 case
if(num.charAt(pivot) == '0' && i != pivot) continue;
Long curr = Long.parseLong(num.substring(pivot, i+1));

if(pivot == 0) {
//no operators present at pivot 0
helper(num, i + 1, curr, curr, path + curr, target, res);
} else {
//three cases
//case 1: +
helper(num, i+1, calc + curr, curr, path + "+" + curr, target, res);
//case 2: -
helper(num, i+1, calc - curr, -curr, path + "-" + curr, target, res);
//case 3: *
helper(num, i+1, calc -tail + (tail * curr), tail * curr, path + "*" + curr, target, res);
}

}
}
}