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