-
Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathMaximumSumOfNonAdjacentElements.java
95 lines (76 loc) · 3.1 KB
/
MaximumSumOfNonAdjacentElements.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.thealgorithms.dynamicprogramming;
/**
* Class to find the maximum sum of non-adjacent elements in an array. This
* class contains two approaches: one with O(n) space complexity and another
* with O(1) space optimization. For more information, refer to
* https://takeuforward.org/data-structure/maximum-sum-of-non-adjacent-elements-dp-5/
*/
final class MaximumSumOfNonAdjacentElements {
private MaximumSumOfNonAdjacentElements() {
}
/**
* Approach 1: Uses a dynamic programming array to store the maximum sum at
* each index. Time Complexity: O(n) - where n is the length of the input
* array. Space Complexity: O(n) - due to the additional dp array.
* @param arr The input array of integers.
* @return The maximum sum of non-adjacent elements.
*/
public static int getMaxSumApproach1(int[] arr) {
if (arr.length == 0) {
return 0; // Check for empty array
}
int n = arr.length;
int[] dp = new int[n];
// Base case: Maximum sum if only one element is present.
dp[0] = arr[0];
for (int ind = 1; ind < n; ind++) {
// Case 1: Do not take the current element, carry forward the previous max
// sum.
int notTake = dp[ind - 1];
// Case 2: Take the current element, add it to the max sum up to two
// indices before.
int take = arr[ind];
if (ind > 1) {
take += dp[ind - 2];
}
// Store the maximum of both choices in the dp array.
dp[ind] = Math.max(take, notTake);
}
return dp[n - 1];
}
/**
* Approach 2: Optimized space complexity approach using two variables instead
* of an array. Time Complexity: O(n) - where n is the length of the input
* array. Space Complexity: O(1) - as it only uses constant space for two
* variables.
* @param arr The input array of integers.
* @return The maximum sum of non-adjacent elements.
*/
public static int getMaxSumApproach2(int[] arr) {
if (arr.length == 0) {
return 0; // Check for empty array
}
int n = arr.length;
// Two variables to keep track of previous two results:
// prev1 = max sum up to the last element (n-1)
// prev2 = max sum up to the element before last (n-2)
int prev1 = arr[0]; // Base case: Maximum sum for the first element.
int prev2 = 0;
for (int ind = 1; ind < n; ind++) {
// Case 1: Do not take the current element, keep the last max sum.
int notTake = prev1;
// Case 2: Take the current element and add it to the result from two
// steps back.
int take = arr[ind];
if (ind > 1) {
take += prev2;
}
// Calculate the current maximum sum and update previous values.
int current = Math.max(take, notTake);
// Shift prev1 and prev2 for the next iteration.
prev2 = prev1;
prev1 = current;
}
return prev1;
}
}