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
51 changes: 51 additions & 0 deletions problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

/**
Time Complexity = Exponential, approximately O(2^N)
Explanation:
For each element, we either choose it or skip it.
This results in exploring all possible combinations recursively.

Space Complexity = O(Target)
Explanation:
The recursion stack and the current path list can grow up to
target / smallest candidate value.

Did this code successfully run on LeetCode : Yes

Any problem you faced while coding this :
Initially struggled to decide when to move the index forward and
when to stay on the same index to allow reuse of elements.
Fixed it by using:
- idx + 1 for "not choose"
- same idx for "choose"
*/

import java.util.*;

class Solution {

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 cases
if (idx == candidates.length || target < 0) {
return;
}
if (target == 0) {
result.add(new ArrayList<>(path));
return;
}
// Not choose current element
helper(candidates, idx + 1, target, path, result);

// Choose current element
path.add(candidates[idx]);
helper(candidates, idx, target - candidates[idx], path, result);
// Backtrack
path.remove(path.size() - 1);
}
}
83 changes: 83 additions & 0 deletions problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
Time Complexity : Exponential, approximately O(4^N)
Explanation:
At each position, we try different partitions of the number
and for each partition we try three operators:
+, -, *
So the total number of recursive states grows exponentially.

Space Complexity : O(N)
Explanation:
Recursion depth can go up to N and StringBuilder path also holds
up to N characters/operators along one recursive path.

Did this code successfully run on LeetCode : Yes

Any problem you faced while coding this :
Initially the difficult part was handling multiplication,
because multiplication has higher precedence than + and -.
Fixed it by keeping track of the previous operand using 'tail'.
So when '*' is used:
calc = calc - tail + tail * curr
Also had to carefully handle numbers with leading zeros
like "05", which are not valid.
*/
class Solution {



List<String> res;

public List<String> addOperators(String num, int target) {

this.res = new LinkedList<>();
helper(num, 0, 0L, 0L, new StringBuilder(), target);
return res;
}

private void helper(String num, int pivot, long calc, long tail, StringBuilder path, int target) {

// Base case
if (pivot == num.length()) {
if (calc == target) {
res.add(path.toString());
}
return;
}

// Try every possible next number
for (int i = pivot; i < num.length(); i++) {

// Skip numbers with leading zero
if (num.charAt(pivot) == '0' && pivot != i) break;

long curr = Long.parseLong(num.substring(pivot, i + 1));

if (pivot == 0) {
// First number, no operator before it
int pl = path.length();
path.append(curr);
helper(num, i + 1, curr, curr, path, target);
path.setLength(pl);
} else {
// Addition
int pl = path.length();
path.append("+").append(curr);
helper(num, i + 1, calc + curr, curr, path, target);
path.setLength(pl);

// Subtraction
pl = path.length();
path.append("-").append(curr);
helper(num, i + 1, calc - curr, -curr, path, target);
path.setLength(pl);

// Multiplication
pl = path.length();
path.append("*").append(curr);
helper(num, i + 1, calc - tail + tail * curr, tail * curr, path, target);
path.setLength(pl);
}
}
}
}