Compare commits

...

2 Commits

6 changed files with 124 additions and 3 deletions

View File

@ -1,5 +1,6 @@
// https://leetcode.com/problems/combination-sum-ii/ // https://leetcode.com/problems/combination-sum-ii/
import java.util.*; i
mport java.util.*;
class CombinationTwo { class CombinationTwo {
@ -13,6 +14,7 @@ class CombinationTwo {
for (int i = currentIndex; i < candidates.length; i++){ for (int i = currentIndex; i < candidates.length; i++){
if (i > currentIndex && candidates[i] == candidates[i-1]) continue; if (i > currentIndex && candidates[i] == candidates[i-1]) continue;
// or i != currentIndex would also work
if (candidates[i] > target) break; if (candidates[i] > target) break;

View File

@ -0,0 +1,73 @@
// https://leetcode.com/problems/combination-sum-ii/
import java.util.*;
class CombinationTwoAlternate {
public static void findCombination(int currentIndex, List<Integer> ds,
int[] candidates, int n, int target,
List<List<Integer>> finalAnswer){
// If target reached
if (target == 0){
finalAnswer.add(new ArrayList<>(ds));
return;
}
// If out of bounds OR target becomes negative
if (currentIndex == n || target < 0){
return;
}
// ---------------- INCLUDE ----------------
if (candidates[currentIndex] <= target){
ds.add(candidates[currentIndex]);
// Move to next index (no reuse allowed)
findCombination(currentIndex + 1, ds, candidates, n,
target - candidates[currentIndex], finalAnswer);
ds.remove(ds.size() - 1);
}
// ---------------- EXCLUDE ----------------
int nextIndex = currentIndex + 1;
// Skip duplicates
while(nextIndex < n && candidates[nextIndex] == candidates[currentIndex]){
nextIndex++;
}
findCombination(nextIndex, ds, candidates, n, target, finalAnswer);
}
public List<List<Integer>> combinationSum2(int[] candidates, int target){
Arrays.sort(candidates);
List<List<Integer>> finalAnswer = new ArrayList<>();
findCombination(0, new ArrayList<>(), candidates,
candidates.length, target, finalAnswer);
return finalAnswer;
}
}
public class Main {
public static void main(String[] args){
int[] candidates = {1,1,1,2,2};
int target = 4;
Solution sol = new Solution();
List<List<Integer>> result = sol.combinationSum2(candidates, target);
System.out.println("Unique Combinations:");
for(List<Integer> list : result){
System.out.println(list);
}
}
}

42
Recursion/SubsetTwo.java Normal file
View File

@ -0,0 +1,42 @@
// https://leetcode.com/problems/subsets-ii/submissions/1909073945/
import java.util.*;
class SubsetTwo {
public void findSubsets(int ind, int[] nums, List<Integer> ds, List<List<Integer>> ansList) {
ansList.add(new ArrayList<>(ds));
for (int i = ind; i < nums.length; i++) {
if (i > ind && nums[i] == nums[i - 1]) continue;
// or i != ind would also work
ds.add(nums[i]);
findSubsets(i + 1, nums, ds, ansList);
ds.remove(ds.size() - 1);
}
}
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> ansList = new ArrayList<>();
findSubsets(0, nums, new ArrayList<>(), ansList);
return ansList;
}
}
public class Main {
public static void main(String[] args) {
// Assumed Input
int[] nums = {1, 2, 2};
Solution sol = new Solution();
List<List<Integer>> result = sol.subsetsWithDup(nums);
System.out.println("Subsets Without Duplicates:");
for (List<Integer> subset : result) {
System.out.println(subset);
}
}
}

View File

@ -1,6 +1,8 @@
// https://leetcode.com/problems/subsets-ii/submissions/1909073945/
import java.util.*; import java.util.*;
class SubsetsWithoutDuplicates { class SubsetTwoAlternate {
public static void printSubset(int currentIndex, List<Integer> ds, int[] nums, int n, List<List<Integer>> finalAnswer){ public static void printSubset(int currentIndex, List<Integer> ds, int[] nums, int n, List<List<Integer>> finalAnswer){
if (currentIndex == n){ if (currentIndex == n){

View File

@ -1,6 +1,8 @@
// https://leetcode.com/problems/subsets/description/
import java.util.*; import java.util.*;
public class Subset { public class SubsetsOne {
public static void printSubset(int currentIndex, List<Integer> ds, int[] nums, int n, List<List<Integer>> finalAnswer){ public static void printSubset(int currentIndex, List<Integer> ds, int[] nums, int n, List<List<Integer>> finalAnswer){
if (currentIndex == n){ if (currentIndex == n){

0
SubsetsTwo.java Normal file
View File