You are viewing a single comment's thread. Return to all comments →
Solution with Java 8 using Map. Is it good or bad?
public class Solution { static Map<Integer, Integer> indexMaxValueMap = new HashMap<>(); static int maxSubsetSum(int[] arr, int currentIndex) { if (currentIndex > arr.length - 1) { return 0; } if (indexMaxValueMap.get(currentIndex) != null) { return indexMaxValueMap.get(currentIndex); } int currentValue = Math.max(0, arr[currentIndex]); currentValue += Math.max(maxSubsetSum(arr, currentIndex + 2), maxSubsetSum(arr, currentIndex + 3)); indexMaxValueMap.put(currentIndex, currentValue); return currentValue; } static int maxSubsetSum(int[] arr) { return Math.max(maxSubsetSum(arr, 0), maxSubsetSum(arr, 1)); }
Seems like cookies are disabled on this browser, please enable them to open this website
Max Array Sum
You are viewing a single comment's thread. Return to all comments →
Solution with Java 8 using Map. Is it good or bad?