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
62 changes: 62 additions & 0 deletions CombinationSum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Time Complexity :O(2^(m+n))
// Space Complexity :O(n)
// Did this code successfully run on Leetcode :yes
import java.util.ArrayList;
import java.util.List;

class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> answer = new ArrayList<>();
List<Integer> path = new ArrayList<>();
helper(candidates, target, 0,path,answer);
return answer;
}
private void helper(int[] candidates, int target, int i,List<Integer> path,List<List<Integer>> answer ){
if(target < 0 || i == candidates.length) return;
if(target == 0){
answer.add(new ArrayList<>(path));
return;
}
//nochoose
helper(candidates, target, i+1, path, answer);
//choose
//action
path.add(candidates[i]);
//recurse
helper(candidates, target - candidates[i], i, path, answer);
//backtrack
path.remove(path.size()-1);
}
}




// Time Complexity :O(2^(m+n))
// Space Complexity :O(n)
// Did this code successfully run on Leetcode :yes
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> answer = new ArrayList<>();
List<Integer> path = new ArrayList<>();
helper(candidates, target, 0, path, answer);
return answer;
}

private void helper(int[] candidates, int target, int pivot, List<Integer> path, List<List<Integer>> answer) {
if (target < 0 || pivot == candidates.length)
return;
if (target == 0) {
answer.add(new ArrayList<>(path));
return;
}
for (int i = pivot; i < candidates.length; i++) {
//action
path.add(candidates[i]);
//recurse
helper(candidates, target - candidates[i], i, path, answer);
//backtrack
path.remove(path.size() - 1);
}
}
}
39 changes: 39 additions & 0 deletions ExpressionAddOperators.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Time Complexity :O(4^n * n)
// Space Complexity :O(n^2)
// Did this code successfully run on Leetcode :yes
// | calc val | tail
// + | calc+curr | curr
// - | calc-curr | -curr
// * | calc-tail+tail*curr | tail*curr

import java.util.ArrayList;
import java.util.List;

class Solution {
public List<String> addOperators(String num, int target) {
List<String> answer = new ArrayList<>();
helper(num, target, 0, 0, 0, "",answer);
return answer;
}
private void helper(String num, int target, int pivot, long calc, long tail, String path, List<String> answer ) {
if (pivot == num.length()) {
if (calc == target) {
answer.add(path);
}
}
for (int i = pivot; i < num.length(); i++) {
if (num.charAt(pivot) == '0' && i != pivot) break; // preceding 0 case
long curr = Long.parseLong(num.substring(pivot, i + 1));
if (pivot == 0) {
helper(num, target, i + 1, curr, curr, path + curr, answer);
} else {
// +
helper(num, target, i + 1, calc + curr, curr, path + "+" + curr, answer);
// -
helper(num, target, i + 1, calc - curr, -curr, path + "-" + curr, answer);
// *
helper(num, target, i + 1, calc - tail + (tail * curr), tail * curr, path + "*" + curr, answer);
}
}
}
}