Skip to content

Commit b53f161

Browse files
committed
Update the formatting SherLockAndCost.java & SherLockAndCostTest.java
1 parent a476756 commit b53f161

File tree

2 files changed

+89
-83
lines changed

2 files changed

+89
-83
lines changed

src/main/java/com/thealgorithms/dynamicprogramming/SherLockAndCost.java

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,46 @@
1111

1212
public final class SherLockAndCost {
1313

14-
/**
15-
* This method takes a list of integers as input and returns the maximum possible sum of absolute differences between adjacent elements in the array.
16-
*
17-
* @param list the input list of integers
18-
* @return the maximum possible sum of absolute differences between adjacent elements in the array
19-
* <p></p>
20-
* Approach:
21-
* I can use dynamic programming to solve this problem efficiently. Let’s break down the approach:
22-
* <p>
23-
* 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.
24-
* 2.) Iterate through the input array from left to right:
25-
* Update dp[i][0] and dp[i][1] based on the previous values and the current element.
26-
* 3.) The final answer is the maximum value between dp[N-1][0] and dp[N-1][1].
27-
* 4.) The time complexity of this method is O(N), where N is the length of the input list.
28-
*/
29-
public static int sherlockAndCostProblem(List<Integer> list) {
30-
31-
if (list == null || list.isEmpty()) {
32-
return 0;
33-
}
34-
35-
int N = list.size();
36-
int[][] dp = new int[N][2];
37-
dp[0][0] = 0;
38-
dp[0][1] = 0;
39-
40-
for (int i = 1; i < N; i++) {
41-
int curr = list.get(i);
42-
int prev = list.get(i - 1);
43-
44-
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prev - 1);
45-
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + curr - 1);
46-
}
47-
48-
return Math.max(dp[N - 1][0], dp[N - 1][1]);
49-
}
14+
/**
15+
* This method takes a list of integers as input and returns the maximum
16+
* possible sum of absolute differences between adjacent elements in the array.
17+
*
18+
* @param list
19+
* the input list of integers
20+
* @return the maximum possible sum of absolute differences between adjacent
21+
* elements in the array
22+
* <p>
23+
* </p>
24+
* Approach: I can use dynamic programming to solve this problem
25+
* efficiently. Let’s break down the approach:
26+
* <p>
27+
* 1.) Initialize two arrays: dp[i][0] and dp[i][1]. These arrays will
28+
* store the maximum sum of absolute differences for the first i
29+
* elements of the input array. 2.) Iterate through the input array from
30+
* left to right: Update dp[i][0] and dp[i][1] based on the previous
31+
* values and the current element. 3.) The final answer is the maximum
32+
* value between dp[N-1][0] and dp[N-1][1]. 4.) The time complexity of
33+
* this method is O(N), where N is the length of the input list.
34+
*/
35+
public static int sherlockAndCostProblem(List<Integer> list) {
36+
37+
if (list == null || list.isEmpty()) {
38+
return 0;
39+
}
40+
41+
int N = list.size();
42+
int[][] dp = new int[N][2];
43+
dp[0][0] = 0;
44+
dp[0][1] = 0;
45+
46+
for (int i = 1; i < N; i++) {
47+
int curr = list.get(i);
48+
int prev = list.get(i - 1);
49+
50+
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prev - 1);
51+
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + curr - 1);
52+
}
53+
54+
return Math.max(dp[N - 1][0], dp[N - 1][1]);
55+
}
5056
}

src/test/java/com/thealgorithms/dynamicprogramming/SherLockAndCostTest.java

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,51 +10,51 @@
1010

1111
public class SherLockAndCostTest {
1212

13-
@Test
14-
public void testPositiveIntegers() {
15-
List<Integer> inputList = Arrays.asList(1, 2, 3, 4, 5);
16-
int result = SherLockAndCost.sherlockAndCostProblem(inputList);
17-
assertEquals(8, result);
18-
}
19-
20-
@Test
21-
public void testNegativeInteger() {
22-
List<Integer> list = Arrays.asList(-1, -2, -3, -4, -5);
23-
int result = SherLockAndCost.sherlockAndCostProblem(list);
24-
assertEquals(0, result);
25-
}
26-
27-
@Test
28-
public void testMixedElements() {
29-
List<Integer> list = Arrays.asList(-1, 1);
30-
int result = SherLockAndCost.sherlockAndCostProblem(list);
31-
assertEquals(0, result);
32-
}
33-
34-
@Test
35-
public void testEdge() {
36-
List<Integer> list = Arrays.asList(-1, 2, 3, 4, 5);
37-
int result = SherLockAndCost.sherlockAndCostProblem(list);
38-
assertEquals(8, result);
39-
}
40-
41-
@Test
42-
public void testNullElements() {
43-
List<Integer> list = Arrays.asList(null, null, null, null);
44-
assertThrows(NullPointerException.class, () -> SherLockAndCost.sherlockAndCostProblem(list));
45-
}
46-
47-
@Test
48-
public void testEmptyList() {
49-
List<Integer> list = Arrays.asList();
50-
int result = SherLockAndCost.sherlockAndCostProblem(list);
51-
assertEquals(0, result);
52-
}
53-
54-
@Test
55-
public void testSingleFList() {
56-
List<Integer> list = Arrays.asList(500);
57-
int result = SherLockAndCost.sherlockAndCostProblem(list);
58-
assertEquals(0, result);
59-
}
13+
@Test
14+
public void testPositiveIntegers() {
15+
List<Integer> inputList = Arrays.asList(1, 2, 3, 4, 5);
16+
int result = SherLockAndCost.sherlockAndCostProblem(inputList);
17+
assertEquals(8, result);
18+
}
19+
20+
@Test
21+
public void testNegativeInteger() {
22+
List<Integer> list = Arrays.asList(-1, -2, -3, -4, -5);
23+
int result = SherLockAndCost.sherlockAndCostProblem(list);
24+
assertEquals(0, result);
25+
}
26+
27+
@Test
28+
public void testMixedElements() {
29+
List<Integer> list = Arrays.asList(-1, 1);
30+
int result = SherLockAndCost.sherlockAndCostProblem(list);
31+
assertEquals(0, result);
32+
}
33+
34+
@Test
35+
public void testEdge() {
36+
List<Integer> list = Arrays.asList(-1, 2, 3, 4, 5);
37+
int result = SherLockAndCost.sherlockAndCostProblem(list);
38+
assertEquals(8, result);
39+
}
40+
41+
@Test
42+
public void testNullElements() {
43+
List<Integer> list = Arrays.asList(null, null, null, null);
44+
assertThrows(NullPointerException.class, () -> SherLockAndCost.sherlockAndCostProblem(list));
45+
}
46+
47+
@Test
48+
public void testEmptyList() {
49+
List<Integer> list = Arrays.asList();
50+
int result = SherLockAndCost.sherlockAndCostProblem(list);
51+
assertEquals(0, result);
52+
}
53+
54+
@Test
55+
public void testSingleFList() {
56+
List<Integer> list = Arrays.asList(500);
57+
int result = SherLockAndCost.sherlockAndCostProblem(list);
58+
assertEquals(0, result);
59+
}
6060
}

0 commit comments

Comments
 (0)