Skip to content

Commit e442ad4

Browse files
Merge branch 'master' into temperature_convertion
2 parents 63e6203 + d28fee9 commit e442ad4

29 files changed

+132
-55
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.thealgorithms.backtracking;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
/** Backtracking: pick/not-pick with reuse of candidates. */
8+
public final class CombinationSum {
9+
private CombinationSum() {
10+
throw new UnsupportedOperationException("Utility class");
11+
}
12+
13+
public static List<List<Integer>> combinationSum(int[] candidates, int target) {
14+
List<List<Integer>> results = new ArrayList<>();
15+
if (candidates == null || candidates.length == 0) {
16+
return results;
17+
}
18+
19+
// Sort to help with pruning duplicates and early termination
20+
Arrays.sort(candidates);
21+
backtrack(candidates, target, 0, new ArrayList<>(), results);
22+
return results;
23+
}
24+
25+
private static void backtrack(int[] candidates, int remaining, int start, List<Integer> combination, List<List<Integer>> results) {
26+
if (remaining == 0) {
27+
// Found valid combination; add a copy
28+
results.add(new ArrayList<>(combination));
29+
return;
30+
}
31+
32+
for (int i = start; i < candidates.length; i++) {
33+
int candidate = candidates[i];
34+
35+
// If candidate is greater than remaining target, further candidates (sorted) will also be too big
36+
if (candidate > remaining) {
37+
break;
38+
}
39+
40+
// include candidate
41+
combination.add(candidate);
42+
// Because we can reuse the same element, we pass i (not i + 1)
43+
backtrack(candidates, remaining - candidate, i, combination, results);
44+
// backtrack: remove last
45+
combination.remove(combination.size() - 1);
46+
}
47+
}
48+
}

src/main/java/com/thealgorithms/backtracking/FloodFill.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ private FloodFill() {
1212
* Get the color at the given coordinates of a 2D image
1313
*
1414
* @param image The image to be filled
15-
* @param x The x co-ordinate of which color is to be obtained
16-
* @param y The y co-ordinate of which color is to be obtained
15+
* @param x The x coordinate of which color is to be obtained
16+
* @param y The y coordinate of which color is to be obtained
1717
*/
1818

1919
public static int getPixel(final int[][] image, final int x, final int y) {
@@ -24,8 +24,8 @@ public static int getPixel(final int[][] image, final int x, final int y) {
2424
* Put the color at the given coordinates of a 2D image
2525
*
2626
* @param image The image to be filled
27-
* @param x The x co-ordinate at which color is to be filled
28-
* @param y The y co-ordinate at which color is to be filled
27+
* @param x The x coordinate at which color is to be filled
28+
* @param y The y coordinate at which color is to be filled
2929
*/
3030
public static void putPixel(final int[][] image, final int x, final int y, final int newColor) {
3131
image[x][y] = newColor;
@@ -35,8 +35,8 @@ public static void putPixel(final int[][] image, final int x, final int y, final
3535
* Fill the 2D image with new color
3636
*
3737
* @param image The image to be filled
38-
* @param x The x co-ordinate at which color is to be filled
39-
* @param y The y co-ordinate at which color is to be filled
38+
* @param x The x coordinate at which color is to be filled
39+
* @param y The y coordinate at which color is to be filled
4040
* @param newColor The new color which to be filled in the image
4141
* @param oldColor The old color which is to be replaced in the image
4242
*/

src/main/java/com/thealgorithms/ciphers/AES.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2738,7 +2738,7 @@ public static BigInteger decrypt(BigInteger cipherText, BigInteger key) {
27382738

27392739
public static void main(String[] args) {
27402740
try (Scanner input = new Scanner(System.in)) {
2741-
System.out.println("Enter (e) letter for encrpyt or (d) letter for decrypt :");
2741+
System.out.println("Enter (e) letter for encrypt or (d) letter for decrypt :");
27422742
char choice = input.nextLine().charAt(0);
27432743
String in;
27442744
switch (choice) {

src/main/java/com/thealgorithms/conversions/TurkishToLatinConversion.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ private TurkishToLatinConversion() {
1616
* 2. Replace all turkish characters with their corresponding latin characters
1717
* 3. Return the converted string
1818
*
19-
* @param param String paramter
19+
* @param param String parameter
2020
* @return String
2121
*/
2222
public static String convertTurkishToLatin(String param) {

src/main/java/com/thealgorithms/datastructures/graphs/BellmanFord.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public void show(int source, int end,
160160
break;
161161
}
162162
}
163-
if (neg == 0) { // Go ahead and show results of computaion
163+
if (neg == 0) { // Go ahead and show results of computation
164164
System.out.println("Distance is: " + dist[end]);
165165
System.out.println("Path followed:");
166166
System.out.print(source + " ");

src/main/java/com/thealgorithms/datastructures/graphs/MatrixGraphs.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ private int[][] adjacency() {
141141
*
142142
* @param from the parent vertex to check for adjacency
143143
* @param to the child vertex to check for adjacency
144-
* @return whether or not the vertices are adjancent
144+
* @return whether or not the vertices are adjacent
145145
*/
146146
private boolean adjacencyOfEdgeDoesExist(int from, int to) {
147147
return (this.adjacency()[from][to] != AdjacencyMatrixGraph.EDGE_NONE);
@@ -162,7 +162,7 @@ public boolean vertexDoesExist(int aVertex) {
162162
*
163163
* @param from the parent vertex to check for adjacency
164164
* @param to the child vertex to check for adjacency
165-
* @return whether or not the vertices are adjancent
165+
* @return whether or not the vertices are adjacent
166166
*/
167167
public boolean edgeDoesExist(int from, int to) {
168168
if (this.vertexDoesExist(from) && this.vertexDoesExist(to)) {

src/main/java/com/thealgorithms/datastructures/heaps/FibonacciHeap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ public class HeapNode {
387387
private HeapNode parent;
388388

389389
/*
390-
* a constructor for a heapNode withe key @param (key)
390+
* a constructor for a heapNode with key @param (key)
391391
* prev == next == this
392392
* parent == child == null
393393
*/

src/main/java/com/thealgorithms/datastructures/queues/PriorityQueues.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* give numbers that are bigger, a higher priority. Queues in theory have no
1010
* fixed size but when using an array implementation it does.
1111
* <p>
12-
* Additional contibutions made by: PuneetTri(https://github.com/PuneetTri)
12+
* Additional contributions made by: PuneetTri(https://github.com/PuneetTri)
1313
*/
1414
class PriorityQueue {
1515

@@ -32,8 +32,8 @@ class PriorityQueue {
3232

3333
PriorityQueue() {
3434
/* If capacity is not defined, default size of 11 would be used
35-
* capacity=max+1 because we cant access 0th element of PQ, and to
36-
* accomodate (max)th elements we need capacity to be max+1.
35+
* capacity=max+1 because we can't access 0th element of PQ, and to
36+
* accommodate (max)th elements we need capacity to be max+1.
3737
* Parent is at position k, child at position (k*2,k*2+1), if we
3838
* use position 0 in our queue, its child would be at:
3939
* (0*2, 0*2+1) -> (0,0). This is why we start at position 1
@@ -127,7 +127,7 @@ public int remove() {
127127
if (isEmpty()) {
128128
throw new RuntimeException("Queue is Empty");
129129
} else {
130-
int max = queueArray[1]; // By defintion of our max-heap, value at queueArray[1] pos is
130+
int max = queueArray[1]; // By definition of our max-heap, value at queueArray[1] pos is
131131
// the greatest
132132

133133
// Swap max and last element

src/main/java/com/thealgorithms/datastructures/trees/AVLSimple.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.thealgorithms.datastructures.trees;
22

33
/*
4-
* Avl is algo that balance itself while adding new alues to tree
4+
* Avl is algo that balance itself while adding new values to tree
55
* by rotating branches of binary tree and make itself Binary seaarch tree
66
* there are four cases which has to tackle
77
* rotating - left right ,left left,right right,right left

src/main/java/com/thealgorithms/datastructures/trees/BSTRecursiveGeneric.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public BSTRecursiveGeneric() {
3030
}
3131

3232
/**
33-
* Displays the tree is a structed format
33+
* Displays the tree is a structured format
3434
*/
3535
public void prettyDisplay() {
3636
prettyDisplay(root, 0);

0 commit comments

Comments
 (0)