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
32 changes: 32 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// https://leetcode.com/problems/combination-sum/
// Time Complexity : O(2^(m+n)) where m is the number of candidates and n is the target value.
// Space Complexity : O(n) where n is the maximum depth of the recursion tree.
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No


class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<>();
helper(candidates, target, 0, new ArrayList<>(), result);
return result;
}
private void helper(int[] candidates, int target, int idx, List<Integer> path, List<List<Integer>> result){
//base case
if(idx == candidates.length || target < 0) return;
if(target == 0){
result.add(new ArrayList(path));
return;
}
// logic
// no choose
helper(candidates, target, idx+1, path, result);
//choose
// action
path.add(candidates[idx]);
// recurse
helper(candidates, target-candidates[idx], idx, path, result);
// backtrack
path.remove(path.size()-1);
}
}
70 changes: 70 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// https://leetcode.com/problems/expression-add-operators/
// Time Complexity : O(4^n) where n is the length of the input string num.
// This is because for each digit, we have 4 choices: add an operator (+, -, *, or no operator).
// Space Complexity : O(n) where n is the length of the input string num.
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No

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

private void helper(String num, int pivot, long calc, long tail, StringBuilder path, int target,
List<String> result) {
//base
if (pivot == num.length()) {
if (calc == target) {
result.add(path.toString());
}
return;
}
//logic
for (int i = pivot; i < num.length(); i++) {
long curr = Long.parseLong(num.substring(pivot, i + 1));
if (num.charAt(pivot) == '0' && pivot != i) {
continue;
}
int le = path.length();
if (pivot == 0) {
//top level
// 1 12 123
// action
path.append(curr);
// recurse
helper(num, i + 1, curr, curr, path, target, result);
// backtrack
path.setLength(le);
} else {
// +
// action
path.append("+");
path.append(curr);
// recurse
helper(num, i + 1, calc + curr, curr, path, target, result);
// backtrack
path.setLength(le);

// -
// action
path.append("-");
path.append(curr);
// recurse
helper(num, i + 1, calc - curr, -curr, path, target, result);
// backtrack
path.setLength(le);

// *
// action
path.append("*");
path.append(curr);
// recurse
helper(num, i + 1, calc - tail + tail * curr, tail * curr, path, target, result);
// backtrack
path.setLength(le);
}
}
}
}