From c9efb674685c3d50d5cd2bf37ce7527d126fd31d Mon Sep 17 00:00:00 2001 From: GouthamGuna Date: Tue, 21 May 2024 20:41:09 +0530 Subject: [PATCH 01/10] initial commit --- .../dynamicprogramming/SherLockAndCost.java | 43 +++++++++++++++++++ .../SherLockAndCostTest.java | 39 +++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 src/main/java/com/thealgorithms/dynamicprogramming/SherLockAndCost.java create mode 100644 src/test/java/com/thealgorithms/dynamicprogramming/SherLockAndCostTest.java diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/SherLockAndCost.java b/src/main/java/com/thealgorithms/dynamicprogramming/SherLockAndCost.java new file mode 100644 index 00000000000..a7b46aa0cef --- /dev/null +++ b/src/main/java/com/thealgorithms/dynamicprogramming/SherLockAndCost.java @@ -0,0 +1,43 @@ +/** + * This class contains a method to solve the Sherlock and Cost problem using dynamic programming. + * The problem is to find the maximum possible sum of absolute differences between adjacent elements in an array. + */ + +package com.thealgorithms.dynamicprogramming; + +import java.util.List; + +public class SherLockAndCost { + + /** + * This method takes a list of integers as input and returns the maximum possible sum of absolute differences between adjacent elements in the array. + * + * @param list the input list of integers + * @return the maximum possible sum of absolute differences between adjacent elements in the array + *

+ * Approach: + * I can use dynamic programming to solve this problem efficiently. Let’s break down the approach: + *

+ * 1.) Initialize two arrays: dp[i][0] and dp[i][1]. These arrays will store the maximum sum of absolute differences for the first i elements of the input array. + * 2.) Iterate through the input array from left to right: + * Update dp[i][0] and dp[i][1] based on the previous values and the current element. + * 3.) The final answer is the maximum value between dp[N-1][0] and dp[N-1][1]. + * 4.) The time complexity of this method is O(N), where N is the length of the input list. + */ + public static int sherlockAndCostProblem(List list) { + int N = list.size(); + int[][] dp = new int[N][2]; + dp[0][0] = 0; + dp[0][1] = 0; + + for (int i = 1; i < N; i++) { + int curr = list.get(i); + int prev = list.get(i - 1); + + dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prev - 1); + dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + curr - 1); + } + + return Math.max(dp[N - 1][0], dp[N - 1][1]); + } +} \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/SherLockAndCostTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/SherLockAndCostTest.java new file mode 100644 index 00000000000..d455d9e716d --- /dev/null +++ b/src/test/java/com/thealgorithms/dynamicprogramming/SherLockAndCostTest.java @@ -0,0 +1,39 @@ +package com.thealgorithms.dynamicprogramming; + +import java.util.Arrays; +import java.util.List; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class SherLockAndCostTest { + + @Test + public void testPositiveIntegers() { + List inputList = Arrays.asList(1, 2, 3, 4, 5); + int result = SherLockAndCost.sherlockAndCostProblem(inputList); + assertEquals(8, result); + } + + @Test + public void testNegativeInteger() { + List list = Arrays.asList(-1, -2, -3, -4, -5); + int result = SherLockAndCost.sherlockAndCostProblem(list); + assertEquals(0, result); + } + + @Test + public void testMixedElements() { + List list = Arrays.asList(-1, 1); + int result = SherLockAndCost.sherlockAndCostProblem(list); + assertEquals(0, result); + } + + @Test + public void testEdge() { + List list = Arrays.asList(-1, 2, 3, 4, 5); + int result = SherLockAndCost.sherlockAndCostProblem(list); + assertEquals(8, result); + } +} \ No newline at end of file From a4767565bb2cc9deaaf4ab32375bbfd6b0334816 Mon Sep 17 00:00:00 2001 From: GouthamGuna Date: Tue, 21 May 2024 22:36:12 +0530 Subject: [PATCH 02/10] Update src class & test class --- .../dynamicprogramming/SherLockAndCost.java | 9 +++++++- .../SherLockAndCostTest.java | 21 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/SherLockAndCost.java b/src/main/java/com/thealgorithms/dynamicprogramming/SherLockAndCost.java index a7b46aa0cef..2d7d2370e65 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/SherLockAndCost.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/SherLockAndCost.java @@ -1,13 +1,15 @@ /** * This class contains a method to solve the Sherlock and Cost problem using dynamic programming. * The problem is to find the maximum possible sum of absolute differences between adjacent elements in an array. + * + * @author gowtham sankar gunasekaran */ package com.thealgorithms.dynamicprogramming; import java.util.List; -public class SherLockAndCost { +public final class SherLockAndCost { /** * This method takes a list of integers as input and returns the maximum possible sum of absolute differences between adjacent elements in the array. @@ -25,6 +27,11 @@ public class SherLockAndCost { * 4.) The time complexity of this method is O(N), where N is the length of the input list. */ public static int sherlockAndCostProblem(List list) { + + if (list == null || list.isEmpty()) { + return 0; + } + int N = list.size(); int[][] dp = new int[N][2]; dp[0][0] = 0; diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/SherLockAndCostTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/SherLockAndCostTest.java index d455d9e716d..d9fe6c251dd 100644 --- a/src/test/java/com/thealgorithms/dynamicprogramming/SherLockAndCostTest.java +++ b/src/test/java/com/thealgorithms/dynamicprogramming/SherLockAndCostTest.java @@ -6,6 +6,7 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; public class SherLockAndCostTest { @@ -36,4 +37,24 @@ public void testEdge() { int result = SherLockAndCost.sherlockAndCostProblem(list); assertEquals(8, result); } + + @Test + public void testNullElements() { + List list = Arrays.asList(null, null, null, null); + assertThrows(NullPointerException.class, () -> SherLockAndCost.sherlockAndCostProblem(list)); + } + + @Test + public void testEmptyList() { + List list = Arrays.asList(); + int result = SherLockAndCost.sherlockAndCostProblem(list); + assertEquals(0, result); + } + + @Test + public void testSingleFList() { + List list = Arrays.asList(500); + int result = SherLockAndCost.sherlockAndCostProblem(list); + assertEquals(0, result); + } } \ No newline at end of file From b53f161bb65eb1e04322b4bfeff522fd2415828f Mon Sep 17 00:00:00 2001 From: Goutham Date: Thu, 23 May 2024 10:33:54 +0530 Subject: [PATCH 03/10] Update the formatting SherLockAndCost.java & SherLockAndCostTest.java --- .../dynamicprogramming/SherLockAndCost.java | 78 ++++++++------- .../SherLockAndCostTest.java | 94 +++++++++---------- 2 files changed, 89 insertions(+), 83 deletions(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/SherLockAndCost.java b/src/main/java/com/thealgorithms/dynamicprogramming/SherLockAndCost.java index 2d7d2370e65..4923d307395 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/SherLockAndCost.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/SherLockAndCost.java @@ -11,40 +11,46 @@ public final class SherLockAndCost { - /** - * This method takes a list of integers as input and returns the maximum possible sum of absolute differences between adjacent elements in the array. - * - * @param list the input list of integers - * @return the maximum possible sum of absolute differences between adjacent elements in the array - *

- * Approach: - * I can use dynamic programming to solve this problem efficiently. Let’s break down the approach: - *

- * 1.) Initialize two arrays: dp[i][0] and dp[i][1]. These arrays will store the maximum sum of absolute differences for the first i elements of the input array. - * 2.) Iterate through the input array from left to right: - * Update dp[i][0] and dp[i][1] based on the previous values and the current element. - * 3.) The final answer is the maximum value between dp[N-1][0] and dp[N-1][1]. - * 4.) The time complexity of this method is O(N), where N is the length of the input list. - */ - public static int sherlockAndCostProblem(List list) { - - if (list == null || list.isEmpty()) { - return 0; - } - - int N = list.size(); - int[][] dp = new int[N][2]; - dp[0][0] = 0; - dp[0][1] = 0; - - for (int i = 1; i < N; i++) { - int curr = list.get(i); - int prev = list.get(i - 1); - - dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prev - 1); - dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + curr - 1); - } - - return Math.max(dp[N - 1][0], dp[N - 1][1]); - } + /** + * This method takes a list of integers as input and returns the maximum + * possible sum of absolute differences between adjacent elements in the array. + * + * @param list + * the input list of integers + * @return the maximum possible sum of absolute differences between adjacent + * elements in the array + *

+ *

+ * Approach: I can use dynamic programming to solve this problem + * efficiently. Let’s break down the approach: + *

+ * 1.) Initialize two arrays: dp[i][0] and dp[i][1]. These arrays will + * store the maximum sum of absolute differences for the first i + * elements of the input array. 2.) Iterate through the input array from + * left to right: Update dp[i][0] and dp[i][1] based on the previous + * values and the current element. 3.) The final answer is the maximum + * value between dp[N-1][0] and dp[N-1][1]. 4.) The time complexity of + * this method is O(N), where N is the length of the input list. + */ + public static int sherlockAndCostProblem(List list) { + + if (list == null || list.isEmpty()) { + return 0; + } + + int N = list.size(); + int[][] dp = new int[N][2]; + dp[0][0] = 0; + dp[0][1] = 0; + + for (int i = 1; i < N; i++) { + int curr = list.get(i); + int prev = list.get(i - 1); + + dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prev - 1); + dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + curr - 1); + } + + return Math.max(dp[N - 1][0], dp[N - 1][1]); + } } \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/SherLockAndCostTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/SherLockAndCostTest.java index d9fe6c251dd..2aa8713d270 100644 --- a/src/test/java/com/thealgorithms/dynamicprogramming/SherLockAndCostTest.java +++ b/src/test/java/com/thealgorithms/dynamicprogramming/SherLockAndCostTest.java @@ -10,51 +10,51 @@ public class SherLockAndCostTest { - @Test - public void testPositiveIntegers() { - List inputList = Arrays.asList(1, 2, 3, 4, 5); - int result = SherLockAndCost.sherlockAndCostProblem(inputList); - assertEquals(8, result); - } - - @Test - public void testNegativeInteger() { - List list = Arrays.asList(-1, -2, -3, -4, -5); - int result = SherLockAndCost.sherlockAndCostProblem(list); - assertEquals(0, result); - } - - @Test - public void testMixedElements() { - List list = Arrays.asList(-1, 1); - int result = SherLockAndCost.sherlockAndCostProblem(list); - assertEquals(0, result); - } - - @Test - public void testEdge() { - List list = Arrays.asList(-1, 2, 3, 4, 5); - int result = SherLockAndCost.sherlockAndCostProblem(list); - assertEquals(8, result); - } - - @Test - public void testNullElements() { - List list = Arrays.asList(null, null, null, null); - assertThrows(NullPointerException.class, () -> SherLockAndCost.sherlockAndCostProblem(list)); - } - - @Test - public void testEmptyList() { - List list = Arrays.asList(); - int result = SherLockAndCost.sherlockAndCostProblem(list); - assertEquals(0, result); - } - - @Test - public void testSingleFList() { - List list = Arrays.asList(500); - int result = SherLockAndCost.sherlockAndCostProblem(list); - assertEquals(0, result); - } + @Test + public void testPositiveIntegers() { + List inputList = Arrays.asList(1, 2, 3, 4, 5); + int result = SherLockAndCost.sherlockAndCostProblem(inputList); + assertEquals(8, result); + } + + @Test + public void testNegativeInteger() { + List list = Arrays.asList(-1, -2, -3, -4, -5); + int result = SherLockAndCost.sherlockAndCostProblem(list); + assertEquals(0, result); + } + + @Test + public void testMixedElements() { + List list = Arrays.asList(-1, 1); + int result = SherLockAndCost.sherlockAndCostProblem(list); + assertEquals(0, result); + } + + @Test + public void testEdge() { + List list = Arrays.asList(-1, 2, 3, 4, 5); + int result = SherLockAndCost.sherlockAndCostProblem(list); + assertEquals(8, result); + } + + @Test + public void testNullElements() { + List list = Arrays.asList(null, null, null, null); + assertThrows(NullPointerException.class, () -> SherLockAndCost.sherlockAndCostProblem(list)); + } + + @Test + public void testEmptyList() { + List list = Arrays.asList(); + int result = SherLockAndCost.sherlockAndCostProblem(list); + assertEquals(0, result); + } + + @Test + public void testSingleFList() { + List list = Arrays.asList(500); + int result = SherLockAndCost.sherlockAndCostProblem(list); + assertEquals(0, result); + } } \ No newline at end of file From e39bbc6f09affebbc75ea70b5634bc509bcfc099 Mon Sep 17 00:00:00 2001 From: Goutham Date: Thu, 23 May 2024 10:38:50 +0530 Subject: [PATCH 04/10] Update the formatting SherLockAndCost.java & SherLockAndCostTest.java --- .../dynamicprogramming/SherLockAndCost.java | 78 +++++++++---------- 1 file changed, 36 insertions(+), 42 deletions(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/SherLockAndCost.java b/src/main/java/com/thealgorithms/dynamicprogramming/SherLockAndCost.java index 4923d307395..2d7d2370e65 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/SherLockAndCost.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/SherLockAndCost.java @@ -11,46 +11,40 @@ public final class SherLockAndCost { - /** - * This method takes a list of integers as input and returns the maximum - * possible sum of absolute differences between adjacent elements in the array. - * - * @param list - * the input list of integers - * @return the maximum possible sum of absolute differences between adjacent - * elements in the array - *

- *

- * Approach: I can use dynamic programming to solve this problem - * efficiently. Let’s break down the approach: - *

- * 1.) Initialize two arrays: dp[i][0] and dp[i][1]. These arrays will - * store the maximum sum of absolute differences for the first i - * elements of the input array. 2.) Iterate through the input array from - * left to right: Update dp[i][0] and dp[i][1] based on the previous - * values and the current element. 3.) The final answer is the maximum - * value between dp[N-1][0] and dp[N-1][1]. 4.) The time complexity of - * this method is O(N), where N is the length of the input list. - */ - public static int sherlockAndCostProblem(List list) { - - if (list == null || list.isEmpty()) { - return 0; - } - - int N = list.size(); - int[][] dp = new int[N][2]; - dp[0][0] = 0; - dp[0][1] = 0; - - for (int i = 1; i < N; i++) { - int curr = list.get(i); - int prev = list.get(i - 1); - - dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prev - 1); - dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + curr - 1); - } - - return Math.max(dp[N - 1][0], dp[N - 1][1]); - } + /** + * This method takes a list of integers as input and returns the maximum possible sum of absolute differences between adjacent elements in the array. + * + * @param list the input list of integers + * @return the maximum possible sum of absolute differences between adjacent elements in the array + *

+ * Approach: + * I can use dynamic programming to solve this problem efficiently. Let’s break down the approach: + *

+ * 1.) Initialize two arrays: dp[i][0] and dp[i][1]. These arrays will store the maximum sum of absolute differences for the first i elements of the input array. + * 2.) Iterate through the input array from left to right: + * Update dp[i][0] and dp[i][1] based on the previous values and the current element. + * 3.) The final answer is the maximum value between dp[N-1][0] and dp[N-1][1]. + * 4.) The time complexity of this method is O(N), where N is the length of the input list. + */ + public static int sherlockAndCostProblem(List list) { + + if (list == null || list.isEmpty()) { + return 0; + } + + int N = list.size(); + int[][] dp = new int[N][2]; + dp[0][0] = 0; + dp[0][1] = 0; + + for (int i = 1; i < N; i++) { + int curr = list.get(i); + int prev = list.get(i - 1); + + dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prev - 1); + dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + curr - 1); + } + + return Math.max(dp[N - 1][0], dp[N - 1][1]); + } } \ No newline at end of file From 724b151fbc1bc44c85109adb919c0e47895c0e93 Mon Sep 17 00:00:00 2001 From: Goutham Date: Thu, 23 May 2024 10:39:12 +0530 Subject: [PATCH 05/10] Update the formatting SherLockAndCostTest.java --- .../SherLockAndCostTest.java | 94 +++++++++---------- 1 file changed, 47 insertions(+), 47 deletions(-) diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/SherLockAndCostTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/SherLockAndCostTest.java index 2aa8713d270..d9fe6c251dd 100644 --- a/src/test/java/com/thealgorithms/dynamicprogramming/SherLockAndCostTest.java +++ b/src/test/java/com/thealgorithms/dynamicprogramming/SherLockAndCostTest.java @@ -10,51 +10,51 @@ public class SherLockAndCostTest { - @Test - public void testPositiveIntegers() { - List inputList = Arrays.asList(1, 2, 3, 4, 5); - int result = SherLockAndCost.sherlockAndCostProblem(inputList); - assertEquals(8, result); - } - - @Test - public void testNegativeInteger() { - List list = Arrays.asList(-1, -2, -3, -4, -5); - int result = SherLockAndCost.sherlockAndCostProblem(list); - assertEquals(0, result); - } - - @Test - public void testMixedElements() { - List list = Arrays.asList(-1, 1); - int result = SherLockAndCost.sherlockAndCostProblem(list); - assertEquals(0, result); - } - - @Test - public void testEdge() { - List list = Arrays.asList(-1, 2, 3, 4, 5); - int result = SherLockAndCost.sherlockAndCostProblem(list); - assertEquals(8, result); - } - - @Test - public void testNullElements() { - List list = Arrays.asList(null, null, null, null); - assertThrows(NullPointerException.class, () -> SherLockAndCost.sherlockAndCostProblem(list)); - } - - @Test - public void testEmptyList() { - List list = Arrays.asList(); - int result = SherLockAndCost.sherlockAndCostProblem(list); - assertEquals(0, result); - } - - @Test - public void testSingleFList() { - List list = Arrays.asList(500); - int result = SherLockAndCost.sherlockAndCostProblem(list); - assertEquals(0, result); - } + @Test + public void testPositiveIntegers() { + List inputList = Arrays.asList(1, 2, 3, 4, 5); + int result = SherLockAndCost.sherlockAndCostProblem(inputList); + assertEquals(8, result); + } + + @Test + public void testNegativeInteger() { + List list = Arrays.asList(-1, -2, -3, -4, -5); + int result = SherLockAndCost.sherlockAndCostProblem(list); + assertEquals(0, result); + } + + @Test + public void testMixedElements() { + List list = Arrays.asList(-1, 1); + int result = SherLockAndCost.sherlockAndCostProblem(list); + assertEquals(0, result); + } + + @Test + public void testEdge() { + List list = Arrays.asList(-1, 2, 3, 4, 5); + int result = SherLockAndCost.sherlockAndCostProblem(list); + assertEquals(8, result); + } + + @Test + public void testNullElements() { + List list = Arrays.asList(null, null, null, null); + assertThrows(NullPointerException.class, () -> SherLockAndCost.sherlockAndCostProblem(list)); + } + + @Test + public void testEmptyList() { + List list = Arrays.asList(); + int result = SherLockAndCost.sherlockAndCostProblem(list); + assertEquals(0, result); + } + + @Test + public void testSingleFList() { + List list = Arrays.asList(500); + int result = SherLockAndCost.sherlockAndCostProblem(list); + assertEquals(0, result); + } } \ No newline at end of file From f0e5015d250e03c88070a36e7954200f7261af26 Mon Sep 17 00:00:00 2001 From: Goutham Date: Thu, 23 May 2024 12:05:04 +0530 Subject: [PATCH 06/10] Update the formatting sherLockAndCost.java & SherLockAndCostTest.java from vs --- .../thealgorithms/dynamicprogramming/SherLockAndCost.java | 2 +- .../dynamicprogramming/SherLockAndCostTest.java | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/SherLockAndCost.java b/src/main/java/com/thealgorithms/dynamicprogramming/SherLockAndCost.java index 2d7d2370e65..08496c57b11 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/SherLockAndCost.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/SherLockAndCost.java @@ -47,4 +47,4 @@ public static int sherlockAndCostProblem(List list) { return Math.max(dp[N - 1][0], dp[N - 1][1]); } -} \ No newline at end of file +} diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/SherLockAndCostTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/SherLockAndCostTest.java index d9fe6c251dd..a36012d71ba 100644 --- a/src/test/java/com/thealgorithms/dynamicprogramming/SherLockAndCostTest.java +++ b/src/test/java/com/thealgorithms/dynamicprogramming/SherLockAndCostTest.java @@ -1,12 +1,12 @@ package com.thealgorithms.dynamicprogramming; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.util.Arrays; import java.util.List; - import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; public class SherLockAndCostTest { @@ -57,4 +57,4 @@ public void testSingleFList() { int result = SherLockAndCost.sherlockAndCostProblem(list); assertEquals(0, result); } -} \ No newline at end of file +} From 4ccc8a05f3cc87bb214edfde0ef12c5b78f19c6e Mon Sep 17 00:00:00 2001 From: Goutham Date: Thu, 23 May 2024 12:11:13 +0530 Subject: [PATCH 07/10] Update the formatting SherLockAndCostTest.java from vs --- .../thealgorithms/dynamicprogramming/SherLockAndCostTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/SherLockAndCostTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/SherLockAndCostTest.java index a36012d71ba..fade6577565 100644 --- a/src/test/java/com/thealgorithms/dynamicprogramming/SherLockAndCostTest.java +++ b/src/test/java/com/thealgorithms/dynamicprogramming/SherLockAndCostTest.java @@ -7,7 +7,6 @@ import java.util.List; import org.junit.jupiter.api.Test; - public class SherLockAndCostTest { @Test From c2759116ccf465c3b39b659fe32bc33261cda115 Mon Sep 17 00:00:00 2001 From: Goutham Date: Thu, 23 May 2024 12:18:21 +0530 Subject: [PATCH 08/10] Update Impl Private Constructor in SherLockAndCost.java --- .../com/thealgorithms/dynamicprogramming/SherLockAndCost.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/SherLockAndCost.java b/src/main/java/com/thealgorithms/dynamicprogramming/SherLockAndCost.java index 08496c57b11..1ea21d96a9d 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/SherLockAndCost.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/SherLockAndCost.java @@ -10,6 +10,8 @@ import java.util.List; public final class SherLockAndCost { + private SherLockAndCost() { + } /** * This method takes a list of integers as input and returns the maximum possible sum of absolute differences between adjacent elements in the array. From fef98162ebfe0aa2ae247d967b002dcd9ba00e9c Mon Sep 17 00:00:00 2001 From: GouthamGuna Date: Fri, 31 May 2024 08:40:10 +0530 Subject: [PATCH 09/10] initial commit for MultistageGraph.java & MultistageGraphTest.java --- .../graphs/MultistageGraph.java | 74 +++++++++++++++++++ .../graphs/MultistageGraphTest.java | 53 +++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 src/main/java/com/thealgorithms/datastructures/graphs/MultistageGraph.java create mode 100644 src/test/java/com/thealgorithms/datastructures/graphs/MultistageGraphTest.java diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/MultistageGraph.java b/src/main/java/com/thealgorithms/datastructures/graphs/MultistageGraph.java new file mode 100644 index 00000000000..613c679e018 --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/graphs/MultistageGraph.java @@ -0,0 +1,74 @@ +package com.thealgorithms.datastructures.graphs; + +import java.util.ArrayList; +import java.util.List; + +public final class MultistageGraph { + + private MultistageGraph() { + + } + private int k; // number of partitions (k > 2) + + // Adjacency list to store edges between different stages + public List> adjacencyList; + + public MultistageGraph(int k) { + this.k = k; + this.adjacencyList = new ArrayList<>(); + + // Initialize the adjacency list with empty lists for each stage + for (int i = 0; i < k; i++) { + adjacencyList.add(new ArrayList<>()); + } + } + + public void addEdge(int fromStage, int toStage) throws IllegalArgumentException { + if (!isValidStageNumber(fromStage)) { + throw new IllegalArgumentException("Invalid stage number: " + fromStage); + } + + if (!isValidStageNumber(toStage)) { + throw new IllegalArgumentException("Invalid stage number: " + toStage); + } + + // Check if the edge exists between the given stages, and add it if not + if (adjacencyList.get(fromStage - 1).contains(toStage)) { + System.out.println("Edge already exists between stage " + fromStage + " and stage " + toStage); + } else { + adjacencyList.get(fromStage - 1).add(toStage); + System.out.println("Added edge between stage " + fromStage + " and stage " + toStage); + } + } + + private boolean isValidStageNumber(int stage) { + return (stage >= 1 && stage <= k); + } + + public void printGraph() { + System.out.println("Multistage Graph:"); + + for (int i = 0; i < k; i++) { + System.out.print(i + " -> "); + + for (Integer adjacentStage : adjacencyList.get(i)) { + if (!adjacentStage.equals(i)) { + System.out.println(" -> " + adjacentStage); + } + } + } + } + + public static void main(String[] args) { + MultistageGraph graph = new MultistageGraph(4); // Create a multistage graph with k = 4 partitions + + try { + graph.addEdge(1, 2); + graph.addEdge(1, 3); + graph.addEdge(2, 3); + graph.printGraph(); + } catch (IllegalArgumentException e) { + System.out.println("Error: " + e.getMessage()); + } + } +} diff --git a/src/test/java/com/thealgorithms/datastructures/graphs/MultistageGraphTest.java b/src/test/java/com/thealgorithms/datastructures/graphs/MultistageGraphTest.java new file mode 100644 index 00000000000..a3a6236d894 --- /dev/null +++ b/src/test/java/com/thealgorithms/datastructures/graphs/MultistageGraphTest.java @@ -0,0 +1,53 @@ +package com.thealgorithms.datastructures.graphs; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.Arrays; +import java.util.List; + +import org.junit.jupiter.api.Test; + +public class MultistageGraphTest { + +@Test +public void testAddEdge_invalidFromStage() { + MultistageGraph graph = new MultistageGraph(3); + + assertThrows(IllegalArgumentException.class, () -> graph.addEdge(0, 2)); +} + +@Test +public void testAddEdge_invalidToStage() { + MultistageGraph graph = new MultistageGraph(3); + + assertThrows(IllegalArgumentException.class, () -> graph.addEdge(1, 4)); +} + +@Test +public void testAddEdge_edgeAlreadyExists() { + MultistageGraph graph = new MultistageGraph(3); + + graph.addEdge(1, 2); + graph.addEdge(1, 2); // Attempt to add the same edge again + + List expectedAdjacentStages = Arrays.asList(2); + assertEquals(expectedAdjacentStages, graph.adjacencyList.get(0)); +} + +@Test +public void testAddEdge_printsSuccessMessage() { + MultistageGraph graph = new MultistageGraph(3); + + // Capture the standard output stream + ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outputStreamCaptor)); + + graph.addEdge(1, 2); + + String expectedOutput = "Added edge between stage 1 and stage 2\r\n"; + assertEquals(expectedOutput, outputStreamCaptor.toString()); +} +} From 05ca8e3a37fa1edaae31d494c7ef1bc733c1d3b3 Mon Sep 17 00:00:00 2001 From: Goutham Date: Fri, 31 May 2024 10:00:36 +0530 Subject: [PATCH 10/10] Update MultistageGraph.java & MultistageGraphTest.java --- .../graphs/MultistageGraph.java | 5 +- .../graphs/MultistageGraphTest.java | 57 +++++++++---------- 2 files changed, 30 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/MultistageGraph.java b/src/main/java/com/thealgorithms/datastructures/graphs/MultistageGraph.java index 613c679e018..c957da8ecd5 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/MultistageGraph.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/MultistageGraph.java @@ -4,11 +4,10 @@ import java.util.List; public final class MultistageGraph { - + private MultistageGraph() { - } - private int k; // number of partitions (k > 2) + private int k; // number of partitions (k > 2) // Adjacency list to store edges between different stages public List> adjacencyList; diff --git a/src/test/java/com/thealgorithms/datastructures/graphs/MultistageGraphTest.java b/src/test/java/com/thealgorithms/datastructures/graphs/MultistageGraphTest.java index a3a6236d894..54bcac7296f 100644 --- a/src/test/java/com/thealgorithms/datastructures/graphs/MultistageGraphTest.java +++ b/src/test/java/com/thealgorithms/datastructures/graphs/MultistageGraphTest.java @@ -7,47 +7,46 @@ import java.io.PrintStream; import java.util.Arrays; import java.util.List; - import org.junit.jupiter.api.Test; public class MultistageGraphTest { -@Test -public void testAddEdge_invalidFromStage() { - MultistageGraph graph = new MultistageGraph(3); + @Test + public void testAddEdge_invalidFromStage() { + MultistageGraph graph = new MultistageGraph(3); - assertThrows(IllegalArgumentException.class, () -> graph.addEdge(0, 2)); -} + assertThrows(IllegalArgumentException.class, () -> graph.addEdge(0, 2)); + } -@Test -public void testAddEdge_invalidToStage() { - MultistageGraph graph = new MultistageGraph(3); + @Test + public void testAddEdge_invalidToStage() { + MultistageGraph graph = new MultistageGraph(3); - assertThrows(IllegalArgumentException.class, () -> graph.addEdge(1, 4)); -} + assertThrows(IllegalArgumentException.class, () -> graph.addEdge(1, 4)); + } -@Test -public void testAddEdge_edgeAlreadyExists() { - MultistageGraph graph = new MultistageGraph(3); + @Test + public void testAddEdge_edgeAlreadyExists() { + MultistageGraph graph = new MultistageGraph(3); - graph.addEdge(1, 2); - graph.addEdge(1, 2); // Attempt to add the same edge again + graph.addEdge(1, 2); + graph.addEdge(1, 2); // Attempt to add the same edge again - List expectedAdjacentStages = Arrays.asList(2); - assertEquals(expectedAdjacentStages, graph.adjacencyList.get(0)); -} + List expectedAdjacentStages = Arrays.asList(2); + assertEquals(expectedAdjacentStages, graph.adjacencyList.get(0)); + } -@Test -public void testAddEdge_printsSuccessMessage() { - MultistageGraph graph = new MultistageGraph(3); + @Test + public void testAddEdge_printsSuccessMessage() { + MultistageGraph graph = new MultistageGraph(3); - // Capture the standard output stream - ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream(); - System.setOut(new PrintStream(outputStreamCaptor)); + // Capture the standard output stream + ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outputStreamCaptor)); - graph.addEdge(1, 2); + graph.addEdge(1, 2); - String expectedOutput = "Added edge between stage 1 and stage 2\r\n"; - assertEquals(expectedOutput, outputStreamCaptor.toString()); -} + String expectedOutput = "Added edge between stage 1 and stage 2\r\n"; + assertEquals(expectedOutput, outputStreamCaptor.toString()); + } }