Skip to content

Commit e39bbc6

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

File tree

1 file changed

+36
-42
lines changed

1 file changed

+36
-42
lines changed

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

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

1212
public final class SherLockAndCost {
1313

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-
}
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+
}
5650
}

0 commit comments

Comments
 (0)