Tested against: https://leetcode.com/problems/coin-change/ + *
Tested against: https://leetcode.com/problems/coin-change + * + *
Run locally: + * + *
./gradlew run -Palgorithm=dp.CoinChange
*
* @author William Fiset, william.alexandre.fiset@gmail.com
*/
package com.williamfiset.algorithms.dp;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+
public class CoinChange {
- private static final int INF = 987654321;
+ public static class Solution {
+ // Contains the minimum number of coins to make a certain amount, if a solution exists.
+ Optional Run: $ java -cp src/main/java com/williamfiset/algorithms/dp/examples/HouseRobber
*/
-package com.williamfiset.algorithms.dp.examples;
+package com.williamfiset.algorithms.dp.examples.houserobber;
import java.util.*;
diff --git a/src/main/java/com/williamfiset/algorithms/dp/examples/magicalcows/MagicalCows.java b/src/main/java/com/williamfiset/algorithms/dp/examples/magicalcows/MagicalCows.java
index c14f9306f..4805c4813 100644
--- a/src/main/java/com/williamfiset/algorithms/dp/examples/magicalcows/MagicalCows.java
+++ b/src/main/java/com/williamfiset/algorithms/dp/examples/magicalcows/MagicalCows.java
@@ -1,3 +1,5 @@
+package com.williamfiset.algorithms.dp.examples.magicalcows;
+
/**
* Solution to Magical Cows (https://open.kattis.com/problems/magicalcows)
*
diff --git a/src/main/java/com/williamfiset/algorithms/dp/examples/narrowartgallery/NarrowArtGalleryRecursive.java b/src/main/java/com/williamfiset/algorithms/dp/examples/narrowartgallery/NarrowArtGalleryRecursive.java
index b0cdac391..17af39dfb 100644
--- a/src/main/java/com/williamfiset/algorithms/dp/examples/narrowartgallery/NarrowArtGalleryRecursive.java
+++ b/src/main/java/com/williamfiset/algorithms/dp/examples/narrowartgallery/NarrowArtGalleryRecursive.java
@@ -1,3 +1,5 @@
+package com.williamfiset.algorithms.dp.examples.narrowartgallery;
+
/**
* Solution to the Narrow Art Gallery problem from the 2014 ICPC North America Qualifier
*
@@ -47,9 +49,9 @@ public static int f(int k, int r, int c) {
return INF;
}
// Return the value of this subproblem, if it's already been computed.
- // if (dp[k][r][c] != null) {
- // return dp[k][r][c];
- // }
+ if (dp[k][r][c] != null) {
+ return dp[k][r][c];
+ }
// Get the value of the current room at row `r` and column `c`.
int roomValue = gallery[r][c];
return dp[k][r][c] =
diff --git a/src/main/java/com/williamfiset/algorithms/dp/examples/scenes/Scenes.java b/src/main/java/com/williamfiset/algorithms/dp/examples/scenes/Scenes.java
index b852d40ec..1f2faf413 100644
--- a/src/main/java/com/williamfiset/algorithms/dp/examples/scenes/Scenes.java
+++ b/src/main/java/com/williamfiset/algorithms/dp/examples/scenes/Scenes.java
@@ -1,3 +1,5 @@
+package com.williamfiset.algorithms.dp.examples.scenes;
+
/**
* Solution to the Mountain Scenes problem (https://open.kattis.com/problems/scenes)
*
diff --git a/src/main/java/com/williamfiset/algorithms/dp/examples/tilingdominoes/TilingDominoes.java b/src/main/java/com/williamfiset/algorithms/dp/examples/tilingdominoes/TilingDominoes.java
index 4f57a8ab1..491bee341 100644
--- a/src/main/java/com/williamfiset/algorithms/dp/examples/tilingdominoes/TilingDominoes.java
+++ b/src/main/java/com/williamfiset/algorithms/dp/examples/tilingdominoes/TilingDominoes.java
@@ -1,3 +1,5 @@
+package com.williamfiset.algorithms.dp.examples.tilingdominoes;
+
/**
* Solution to Tri Tiling (https://open.kattis.com/problems/tritiling)
*
diff --git a/src/main/java/com/williamfiset/algorithms/graphtheory/Boruvkas.java b/src/main/java/com/williamfiset/algorithms/graphtheory/Boruvkas.java
index 40dbfa84e..1be3a5f7d 100644
--- a/src/main/java/com/williamfiset/algorithms/graphtheory/Boruvkas.java
+++ b/src/main/java/com/williamfiset/algorithms/graphtheory/Boruvkas.java
@@ -17,6 +17,7 @@ public Edge(int u, int v, int cost) {
public String toString() {
return String.format("%d %d, cost: %d", u, v, cost);
}
+
// @Override
public int compareTo(Edge other) {
int cmp = cost - other.cost;
diff --git a/src/main/java/com/williamfiset/algorithms/graphtheory/ConnectedComponentsDfsSolverAdjacencyList.java b/src/main/java/com/williamfiset/algorithms/graphtheory/ConnectedComponentsDfsSolverAdjacencyList.java
index 7ee545ada..827945121 100644
--- a/src/main/java/com/williamfiset/algorithms/graphtheory/ConnectedComponentsDfsSolverAdjacencyList.java
+++ b/src/main/java/com/williamfiset/algorithms/graphtheory/ConnectedComponentsDfsSolverAdjacencyList.java
@@ -19,7 +19,9 @@ public class ConnectedComponentsDfsSolverAdjacencyList {
private boolean[] visited;
private List> graph;
- /** @param graph - An undirected graph as an adjacency list. */
+ /**
+ * @param graph - An undirected graph as an adjacency list.
+ */
public ConnectedComponentsDfsSolverAdjacencyList(List
> graph) {
if (graph == null) throw new NullPointerException();
this.n = graph.size();
diff --git a/src/main/java/com/williamfiset/algorithms/graphtheory/KruskalsEdgeListPartialSortSolver.java b/src/main/java/com/williamfiset/algorithms/graphtheory/KruskalsEdgeListPartialSortSolver.java
index e1742176c..b3a651014 100644
--- a/src/main/java/com/williamfiset/algorithms/graphtheory/KruskalsEdgeListPartialSortSolver.java
+++ b/src/main/java/com/williamfiset/algorithms/graphtheory/KruskalsEdgeListPartialSortSolver.java
@@ -17,12 +17,14 @@ public class KruskalsEdgeListPartialSortSolver {
static class Edge implements Comparable
> g, int[] order) {
return true;
}
- @Test(expected = IllegalArgumentException.class)
+ @Test
public void cycleInGraph() {
List
> g = Utils.createEmptyAdjacencyList(4);
Utils.addDirectedEdge(g, 0, 1);
@@ -48,7 +49,7 @@ public void cycleInGraph() {
Utils.addDirectedEdge(g, 2, 3);
Utils.addDirectedEdge(g, 3, 0);
Kahns solver = new Kahns();
- solver.kahns(g);
+ assertThrows(IllegalArgumentException.class, () -> solver.kahns(g));
}
@Test
diff --git a/src/test/java/com/williamfiset/algorithms/graphtheory/KosarajuTest.java b/src/test/java/com/williamfiset/algorithms/graphtheory/KosarajuTest.java
index c048e83a7..3ce6974c7 100644
--- a/src/test/java/com/williamfiset/algorithms/graphtheory/KosarajuTest.java
+++ b/src/test/java/com/williamfiset/algorithms/graphtheory/KosarajuTest.java
@@ -6,10 +6,11 @@
package com.williamfiset.algorithms.graphtheory;
import static com.google.common.truth.Truth.assertThat;
+import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
import com.google.common.collect.ImmutableList;
import java.util.*;
-import org.junit.*;
+import org.junit.jupiter.api.*;
public class KosarajuTest {
@@ -25,9 +26,9 @@ public static void addEdge(List
> graph, int from, int to) {
graph.get(from).add(to);
}
- @Test(expected = IllegalArgumentException.class)
+ @Test
public void nullGraphConstructor() {
- new Kosaraju(null);
+ assertThrowsExactly(IllegalArgumentException.class, () -> new Kosaraju(null));
}
@Test
diff --git a/src/test/java/com/williamfiset/algorithms/graphtheory/SteinerTreeTest.java b/src/test/java/com/williamfiset/algorithms/graphtheory/SteinerTreeTest.java
index ea003fcc7..d4713841d 100644
--- a/src/test/java/com/williamfiset/algorithms/graphtheory/SteinerTreeTest.java
+++ b/src/test/java/com/williamfiset/algorithms/graphtheory/SteinerTreeTest.java
@@ -2,7 +2,7 @@
import static com.google.common.truth.Truth.assertThat;
-import org.junit.*;
+import org.junit.jupiter.api.*;
public class SteinerTreeTest {
diff --git a/src/test/java/com/williamfiset/algorithms/graphtheory/TarjanSccSolverAdjacencyListTest.java b/src/test/java/com/williamfiset/algorithms/graphtheory/TarjanSccSolverAdjacencyListTest.java
index 0ccd37318..cab07eebf 100644
--- a/src/test/java/com/williamfiset/algorithms/graphtheory/TarjanSccSolverAdjacencyListTest.java
+++ b/src/test/java/com/williamfiset/algorithms/graphtheory/TarjanSccSolverAdjacencyListTest.java
@@ -1,10 +1,11 @@
package com.williamfiset.algorithms.graphtheory;
import static com.google.common.truth.Truth.assertThat;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import com.google.common.collect.ImmutableList;
import java.util.*;
-import org.junit.*;
+import org.junit.jupiter.api.*;
public class TarjanSccSolverAdjacencyListTest {
@@ -20,9 +21,9 @@ public static void addEdge(List
> graph, int from, int to) {
graph.get(from).add(to);
}
- @Test(expected = IllegalArgumentException.class)
+ @Test
public void nullGraphConstructor() {
- new TarjanSccSolverAdjacencyList(null);
+ assertThrows(IllegalArgumentException.class, () -> new TarjanSccSolverAdjacencyList(null));
}
@Test
diff --git a/src/test/java/com/williamfiset/algorithms/graphtheory/TravelingSalesmanProblemTest.java b/src/test/java/com/williamfiset/algorithms/graphtheory/TravelingSalesmanProblemTest.java
index f9b1c2486..5e8909bdd 100644
--- a/src/test/java/com/williamfiset/algorithms/graphtheory/TravelingSalesmanProblemTest.java
+++ b/src/test/java/com/williamfiset/algorithms/graphtheory/TravelingSalesmanProblemTest.java
@@ -1,68 +1,71 @@
package com.williamfiset.algorithms.graphtheory;
import static com.google.common.truth.Truth.assertThat;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.*;
-import org.junit.*;
+import org.junit.jupiter.api.*;
public class TravelingSalesmanProblemTest {
private static final double EPS = 1e-5;
- @Test(expected = IllegalArgumentException.class)
+ @Test
public void testTspRecursiveInvalidStartNode() {
double[][] dist = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
- new TspDynamicProgrammingRecursive(321, dist);
+ assertThrows(
+ IllegalArgumentException.class, () -> new TspDynamicProgrammingRecursive(321, dist));
}
- @Test(expected = IllegalArgumentException.class)
+ @Test
public void testTspIterativeInvalidStartNode() {
double[][] dist = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
- new TspDynamicProgrammingIterative(321, dist);
+ assertThrows(
+ IllegalArgumentException.class, () -> new TspDynamicProgrammingIterative(321, dist));
}
- @Test(expected = IllegalStateException.class)
+ @Test
public void testTspRecursiveNonSquareMatrix() {
double[][] dist = {
{1, 2, 3},
{4, 5, 6}
};
- new TspDynamicProgrammingRecursive(dist);
+ assertThrows(IllegalStateException.class, () -> new TspDynamicProgrammingRecursive(dist));
}
- @Test(expected = IllegalStateException.class)
+ @Test
public void testTspIterativeNonSquareMatrix() {
double[][] dist = {
{1, 2, 3},
{4, 5, 6}
};
- new TspDynamicProgrammingIterative(dist);
+ assertThrows(IllegalStateException.class, () -> new TspDynamicProgrammingIterative(dist));
}
- @Test(expected = IllegalStateException.class)
+ @Test
public void testTspRecursiveSmallGraph() {
double[][] dist = {
{0, 1},
{1, 0}
};
- new TspDynamicProgrammingRecursive(dist);
+ assertThrows(IllegalStateException.class, () -> new TspDynamicProgrammingRecursive(dist));
}
- @Test(expected = IllegalStateException.class)
+ @Test
public void testTspIterativeSmallGraph() {
double[][] dist = {
{0, 1},
{1, 0}
};
- new TspDynamicProgrammingIterative(dist);
+ assertThrows(IllegalStateException.class, () -> new TspDynamicProgrammingIterative(dist));
}
@Test
diff --git a/src/test/java/com/williamfiset/algorithms/graphtheory/TwoSatSolverAdjacencyListTest.java b/src/test/java/com/williamfiset/algorithms/graphtheory/TwoSatSolverAdjacencyListTest.java
index d57d4c404..29eaa0e4b 100644
--- a/src/test/java/com/williamfiset/algorithms/graphtheory/TwoSatSolverAdjacencyListTest.java
+++ b/src/test/java/com/williamfiset/algorithms/graphtheory/TwoSatSolverAdjacencyListTest.java
@@ -3,7 +3,7 @@
import static com.google.common.truth.Truth.assertThat;
import java.util.*;
-import org.junit.*;
+import org.junit.jupiter.api.*;
public class TwoSatSolverAdjacencyListTest {
diff --git a/src/test/java/com/williamfiset/algorithms/graphtheory/networkflow/BipartiteGraphCheckAdjacencyListTest.java b/src/test/java/com/williamfiset/algorithms/graphtheory/networkflow/BipartiteGraphCheckAdjacencyListTest.java
index c0c0c4bd0..4768393ae 100644
--- a/src/test/java/com/williamfiset/algorithms/graphtheory/networkflow/BipartiteGraphCheckAdjacencyListTest.java
+++ b/src/test/java/com/williamfiset/algorithms/graphtheory/networkflow/BipartiteGraphCheckAdjacencyListTest.java
@@ -4,14 +4,14 @@
import com.williamfiset.algorithms.utils.graphutils.Utils;
import java.util.*;
-import org.junit.*;
+import org.junit.jupiter.api.*;
public class BipartiteGraphCheckAdjacencyListTest {
private List
> graph;
private BipartiteGraphCheckAdjacencyList solver;
- @Before
+ @BeforeEach
public void setUp() {}
@Test
diff --git a/src/test/java/com/williamfiset/algorithms/graphtheory/networkflow/MaxFlowTests.java b/src/test/java/com/williamfiset/algorithms/graphtheory/networkflow/MaxFlowTests.java
index a71e33f50..3c9f23f12 100644
--- a/src/test/java/com/williamfiset/algorithms/graphtheory/networkflow/MaxFlowTests.java
+++ b/src/test/java/com/williamfiset/algorithms/graphtheory/networkflow/MaxFlowTests.java
@@ -4,13 +4,13 @@
import com.williamfiset.algorithms.graphtheory.networkflow.NetworkFlowSolverBase.Edge;
import java.util.*;
-import org.junit.*;
+import org.junit.jupiter.api.*;
public class MaxFlowTests {
List