We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
publicstaticintmaxSubsetSum(List<Integer>arr){int[]dp=newint[arr.size()];// First init value of dp[0] and dp[1], if negative then set to 0dp[0]=Math.max(arr.get(0),0);dp[1]=Math.max(dp[0],arr.get(1));for(inti=2;i<dp.length;i++){// If value of dp[i - 2] + arr.get(i) > dp[i - 1] then dp[i] = dp[i - 2] + arr.get(i) else dp[i] = dp[i - 1dp[i]=Math.max(dp[i-2]+arr.get(i),dp[i-1]);}returndp[dp.length-1];}}
Cookie support is required to access HackerRank
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 for Java