Elegant Java Solution


  • 0
    N
    class Solution {
        public int deleteAndEarn(int[] nums) {
            int[] dp = new int[10001];
            
            for (int i : nums) {
                dp[i]++;
            }
        
            for (int i = 2; i < dp.length; ++i) {
                dp[i] = Math.max( dp[i-1], dp[i] * i + dp[i-2] );
            }
            
            return dp[10000];
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.