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
83 changes: 83 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Time Complexity : O(m^2 * n^2) where m is the number of elements in candidates and n is the target
// Space Complexity : O(h) where h is the height of the tree
// 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 iterative approach where we select a pivot and start the index from the pivot to get the combinations of all numbers.
* We are starting the iteration from the pivot because the number can be re-used again.
* <p>
* Base case 1 is when target becomes less than 0. The target number cannot be achieved using the current path so we return.
* Base case 2 is when target becomes 0. The target number is achieved so we add the current path to the result.
*/
class IterativeSolution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<>();
helper(candidates, 0, target, new ArrayList<>(), result);
return result;
}

private void helper(int[] candidates, int pivot, int target, List<Integer> path, List<List<Integer>> result) {
//base
if (target < 0)
return;
if (target == 0) {
result.add(new ArrayList<>(path));
return;
}

for (int i = pivot; i < candidates.length; i++) {
path.add(candidates[i]);
helper(candidates, i, target - candidates[i], path, result);
// backtracking
path.remove(path.size() - 1);
}

}
}

// Time Complexity : O(m^2 * n^2) where m is the number of elements in candidates and n is the target
// Space Complexity : O(h) where h is the height of the tree
// 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 recursive approach where we do the 0 and 1 case where we do not choose or choose the current element.
* When we select the element, we recursively try to find the target sum. Once that recursion is complete, we backtrack the current element from the recursion.
* <p>
* Base case 1 is when the target becomes negative or we reach the end of all elements in the array. We return since target was not found
* Base case 2 is when the target becomes 0. We add the current path to the result.
*/
class RecursiveSolution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<>();
helper(candidates, 0, target, new ArrayList<>(), result);
return result;
}

private void helper(int[] candidates, int idx, int target, List<Integer> path, List<List<Integer>> result) {
//base
if (target < 0 || idx == candidates.length)
return;
if (target == 0) {
result.add(new ArrayList<>(path));
return;
}

// 0
helper(candidates, idx + 1, target, path, result);

// 1
path.add(candidates[idx]);
helper(candidates, idx, target - candidates[idx], path, result);

path.remove(path.size() - 1);

}
}
64 changes: 64 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Time Complexity : 4 ^ n where n is the length of nums string
// 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

/**
* We create possible strings with pivot based recursion. At each level, we apply the operators and generate the current number which will be used by next level.
* When we reach the end of the num string, we check if the target value matches the current value and add it to the result.
*/
class Solution {
public List<String> addOperators(String num, int target) {
List<String> result = new ArrayList<>();
helper(num, target, 0, 0l, 0l, new StringBuilder(), result);
return result;
}

private void helper(String num, int target, int pivot, long calc, long tail, StringBuilder path, List<String> result) {
//if all numbers are taken into consideration
if (pivot == num.length()) {
if (calc == target) {
result.add(path.toString());
}
return;
}

for (int i = pivot; i < num.length(); i++) {
// leading 0 needs to be ignored
if (num.charAt(pivot) == '0' && i != pivot)
return;
long curr = Long.parseLong(num.substring(pivot, i + 1));
int le = path.length();
// first level so the current and tail are the number itself
if (pivot == 0) {
path.append(curr);
helper(num, target, i + 1, curr, curr, path, result);
path.setLength(le);
} else {

// Adding to path, recurse and backtrack for all 3 actions
// +
path.append("+");
path.append(curr);
helper(num, target, i + 1, calc + curr, curr, path, result);
path.setLength(le);

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

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

}
}
}