|
| 1 | +## Purpose |
| 2 | + |
| 3 | +I recently came across an interview question that stumped me for a while |
| 4 | +> Given a list of integers, write a function that returns the largest sum of non-adjacent numbers. |
| 5 | +
|
| 6 | +I am looking for feedback around both my implementation and the correctness of my proposed solution (I have some test |
| 7 | +cases, but I could have missed a case). |
| 8 | + |
| 9 | +## Approach |
| 10 | + |
| 11 | +For arrays with `1` or `2` values, the answer is trivial. |
| 12 | + |
| 13 | +For arrays with more than `2` values, keep track of the maximum sum from the preceding non-adjacent elements that |
| 14 | +*includes the most recent non-adjacent element*, and the maximum sum from the preceding non-adjacent elements that |
| 15 | +*excludes the most recent non-adjacent element*. |
| 16 | + |
| 17 | +So for `[3, 1, 1, 5, 1]` at index `3`, the maximum sum from preceding non-adjacent elements that includes the most recent |
| 18 | +non-adjacent element is `1`, while the maximum sum from preceding non-adjacent elements that excludes the most recent |
| 19 | +non-adjacent element is `3`. |
| 20 | + |
| 21 | +Taking the max of these two sums and adding them to the current value will produce the maximum sum for non-adjacent |
| 22 | +elements up to that index. Continuing this process for each remaining index and then comparing the last two calculated |
| 23 | +sums will produce the maximum sum. |
| 24 | + |
| 25 | +Let's walk through this process with `[3, 1, 1, 5, 1]`. |
| 26 | + |
| 27 | +* At index `0`, the max sum is `3` |
| 28 | +* At index `1`, the max sum is `1` |
| 29 | +* At index `2`, the max sum from including the most recent non-adjacent element is `3`, while the max sum from excluding |
| 30 | + the most recent non-adjacent element is `0`. Thus, we choose `3` and add that to the current value (`1`). `4` is now |
| 31 | + the max non-adjacent sum for index `2`. |
| 32 | +* At index `3`, the max sum from including the most recent non-adjacent element is `1`, while the max sum from excluding |
| 33 | + the most recent non-adjacent element is `3`. Thus, we choose `3` and add that to the current value (`5`). `8` is now |
| 34 | + the max non-adjacent sum for index `3`. |
| 35 | +* At index `4`, the max sum from including the most recent non-adjacent element is `4`, while the max sum from excluding |
| 36 | + the most recent non-adjacent element is `3`. Thus, we choose `4` and add that to the current value (`1`). `6` is now |
| 37 | + the max non-adjacent sum for index `4`. |
| 38 | +* The final two sums were `8` and `6` - we take the max (`8`). |
| 39 | + |
| 40 | +Apologies if this was confusing to follow, it was difficult to describe my thought process. |
| 41 | + |
| 42 | +## Implementation |
| 43 | + |
| 44 | +<!-- language: lang-java --> |
| 45 | + |
| 46 | + public class MaximumNonAdjacentElementSumIdentifier { |
| 47 | + public static int identify(int[] values) { |
| 48 | + if (values == null || values.length == 0) { |
| 49 | + throw new RuntimeException("Unable to identify sum"); |
| 50 | + } |
| 51 | + |
| 52 | + if (values.length == 1) { |
| 53 | + return values[0]; |
| 54 | + } |
| 55 | + |
| 56 | + int firstSum = 0; |
| 57 | + int secondSum = values[0]; |
| 58 | + int previousValueSum = values[1]; |
| 59 | + |
| 60 | + for (int i = 2; i < values.length; i++) { |
| 61 | + int value = values[i]; |
| 62 | + int maximumCurrentElementSum = Math.max(firstSum, secondSum) + value; |
| 63 | + firstSum = Math.max(firstSum, secondSum); |
| 64 | + secondSum = previousValueSum; |
| 65 | + previousValueSum = maximumCurrentElementSum; |
| 66 | + } |
| 67 | + |
| 68 | + return Math.max(secondSum, previousValueSum); |
| 69 | + } |
| 70 | + } |
0 commit comments