Skip to content

Commit 1adbbfb

Browse files
committed
backtracking started
1 parent 12edcb4 commit 1adbbfb

File tree

6 files changed

+138
-29
lines changed

6 files changed

+138
-29
lines changed

.idea/fileTemplates/internal/Class.java

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 35 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,16 @@
2828
<version>3.1.8</version>
2929
</dependency>
3030
</dependencies>
31+
<build>
32+
<plugins>
33+
<plugin>
34+
<groupId>org.apache.maven.plugins</groupId>
35+
<artifactId>maven-compiler-plugin</artifactId>
36+
<configuration>
37+
<source>21</source>
38+
<target>21</target>
39+
</configuration>
40+
</plugin>
41+
</plugins>
42+
</build>
3143
</project>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.subham.ta.backtracking;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class CombinationSumI {
7+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
8+
List<List<Integer>> result = new ArrayList<>();
9+
List<Integer> runningSet = new ArrayList<>();
10+
combinationSum(candidates, candidates.length - 1, target, result, runningSet);
11+
return result;
12+
}
13+
14+
void combinationSum(
15+
int[] arr, int index, int target, List<List<Integer>> result, List<Integer> currentSet) {
16+
if (index < 0 || target < 0) {
17+
return;
18+
}
19+
20+
if (target == 0) {
21+
result.add(new ArrayList<>(currentSet));
22+
return;
23+
}
24+
25+
// include
26+
currentSet.addLast(arr[index]);
27+
combinationSum(arr, index, target - arr[index], result, currentSet);
28+
currentSet.removeLast();
29+
30+
// exclude
31+
combinationSum(arr, index - 1, target, result, currentSet);
32+
}
33+
34+
public static void main(String[] args) {
35+
System.out.println(new CombinationSumI().combinationSum(new int[] {2, 3, 6, 7}, 7));
36+
}
37+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.subham.ta.backtracking;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.stream.Collectors;
7+
8+
/**
9+
* @author: Subham Santra
10+
*/
11+
public class PermutationI {
12+
13+
public List<List<Integer>> permute(int[] nums) {
14+
List<List<Integer>> result = new ArrayList<>();
15+
permute(nums, 0, result);
16+
return result;
17+
}
18+
19+
void permute(int[] arr, int i, List<List<Integer>> result) {
20+
if (i == arr.length) {
21+
result.add(Arrays.stream(arr).boxed().collect(Collectors.toList()));
22+
return;
23+
}
24+
25+
for (int j = i; j < arr.length; ++j) {
26+
swap(arr, i, j);
27+
permute(arr, i + 1, result);
28+
swap(arr, i, j);
29+
}
30+
}
31+
32+
void swap(int[] arr, int f, int t) {
33+
int x = arr[f];
34+
arr[f] = arr[t];
35+
arr[t] = x;
36+
}
37+
38+
public static void main(String[] args) {
39+
System.out.println(new PermutationI().permute(new int[] {0}));
40+
System.out.println(new PermutationI().permute(new int[] {1}));
41+
System.out.println(new PermutationI().permute(new int[] {1, 2}));
42+
System.out.println(new PermutationI().permute(new int[] {1, 2, 3}));
43+
}
44+
}

0 commit comments

Comments
 (0)