Skip to content

Commit c7dcbea

Browse files
committed
format code
1 parent 8efc52e commit c7dcbea

File tree

1 file changed

+77
-77
lines changed

1 file changed

+77
-77
lines changed

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

Lines changed: 77 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -7,81 +7,81 @@
77
* For more information, refer to https://takeuforward.org/data-structure/maximum-sum-of-non-adjacent-elements-dp-5/
88
*/
99
final class MaximumSumOfNonAdjacentElements {
10-
private MaximumSumOfNonAdjacentElements() {
11-
}
12-
13-
/**
14-
* Approach 1: Uses a dynamic programming array to store the maximum sum at each
15-
* index.
16-
* Time Complexity: O(n) - where n is the length of the input array. Space
17-
* Complexity: O(n) - due to the additional dp array.
18-
* @param arr The input array of integers.
19-
* @return The maximum sum of non-adjacent elements.
20-
*/
21-
public static int getMaxSumApproach1(int[] arr) {
22-
int n = arr.length;
23-
int[] dp = new int[n];
24-
25-
// Base case: Maximum sum if only one element is present.
26-
dp[0] = arr[0];
27-
28-
for (int ind = 1; ind < n; ind++) {
29-
30-
// Case 1: Do not take the current element, carry forward the previous max sum.
31-
int notTake = dp[ind - 1];
32-
33-
// Case 2: Take the current element, add it to the max sum up to two indices
34-
// before.
35-
int take = arr[ind];
36-
if (ind > 1) {
37-
take += dp[ind - 2];
38-
}
39-
40-
// Store the maximum of both choices in the dp array.
41-
dp[ind] = Math.max(take, notTake);
42-
}
43-
44-
return dp[n - 1];
45-
}
46-
47-
/**
48-
* Approach 2: Optimized space complexity approach using two variables instead
49-
* of an array.
50-
* Time Complexity: O(n) - where n is the length of the input array. Space
51-
* Complexity: O(1) - as it only uses constant space for two variables.
52-
* @param arr The input array of integers.
53-
* @return The maximum sum of non-adjacent elements.
54-
*/
55-
public static int getMaxSumApproach2(int[] arr) {
56-
int n = arr.length;
57-
58-
// Two variables to keep track of previous two results:
59-
// prev1 = max sum up to the last element (n-1)
60-
// prev2 = max sum up to the element before last (n-2)
61-
62-
int prev1 = arr[0]; // Base case: Maximum sum for the first element.
63-
int prev2 = 0;
64-
65-
for (int ind = 1; ind < n; ind++) {
66-
67-
// Case 1: Do not take the current element, keep the last max sum.
68-
int notTake = prev1;
69-
70-
// Case 2: Take the current element and add it to the result from two steps
71-
// back.
72-
int take = arr[ind];
73-
if (ind > 1) {
74-
take += prev2;
75-
}
76-
77-
// Calculate the current maximum sum and update previous values.
78-
int current = Math.max(take, notTake);
79-
80-
// Shift prev1 and prev2 for the next iteration.
81-
prev2 = prev1;
82-
prev1 = current;
83-
}
84-
85-
return prev1;
86-
}
10+
private MaximumSumOfNonAdjacentElements() {
11+
}
12+
13+
/**
14+
* Approach 1: Uses a dynamic programming array to store the maximum sum at each
15+
* index.
16+
* Time Complexity: O(n) - where n is the length of the input array. Space
17+
* Complexity: O(n) - due to the additional dp array.
18+
* @param arr The input array of integers.
19+
* @return The maximum sum of non-adjacent elements.
20+
*/
21+
public static int getMaxSumApproach1(int[] arr) {
22+
int n = arr.length;
23+
int[] dp = new int[n];
24+
25+
// Base case: Maximum sum if only one element is present.
26+
dp[0] = arr[0];
27+
28+
for (int ind = 1; ind < n; ind++) {
29+
30+
// Case 1: Do not take the current element, carry forward the previous max sum.
31+
int notTake = dp[ind - 1];
32+
33+
// Case 2: Take the current element, add it to the max sum up to two indices
34+
// before.
35+
int take = arr[ind];
36+
if (ind > 1) {
37+
take += dp[ind - 2];
38+
}
39+
40+
// Store the maximum of both choices in the dp array.
41+
dp[ind] = Math.max(take, notTake);
42+
}
43+
44+
return dp[n - 1];
45+
}
46+
47+
/**
48+
* Approach 2: Optimized space complexity approach using two variables instead
49+
* of an array.
50+
* Time Complexity: O(n) - where n is the length of the input array. Space
51+
* Complexity: O(1) - as it only uses constant space for two variables.
52+
* @param arr The input array of integers.
53+
* @return The maximum sum of non-adjacent elements.
54+
*/
55+
public static int getMaxSumApproach2(int[] arr) {
56+
int n = arr.length;
57+
58+
// Two variables to keep track of previous two results:
59+
// prev1 = max sum up to the last element (n-1)
60+
// prev2 = max sum up to the element before last (n-2)
61+
62+
int prev1 = arr[0]; // Base case: Maximum sum for the first element.
63+
int prev2 = 0;
64+
65+
for (int ind = 1; ind < n; ind++) {
66+
67+
// Case 1: Do not take the current element, keep the last max sum.
68+
int notTake = prev1;
69+
70+
// Case 2: Take the current element and add it to the result from two steps
71+
// back.
72+
int take = arr[ind];
73+
if (ind > 1) {
74+
take += prev2;
75+
}
76+
77+
// Calculate the current maximum sum and update previous values.
78+
int current = Math.max(take, notTake);
79+
80+
// Shift prev1 and prev2 for the next iteration.
81+
prev2 = prev1;
82+
prev1 = current;
83+
}
84+
85+
return prev1;
86+
}
8787
}

0 commit comments

Comments
 (0)