Max Array Sum

  • + 1 comment

    Simple TypeScript/JavaScript 4 line solution

    function maxSubsetSum(a: number[]): number {
        const dp: number[] = [];
        for (let i = 0; i < a.length; i++)
            dp[i] = Math.max(a[i], i == 0 ? 0 : dp[i - 1], a[i] + (i < 2 ? 0 : dp[i - 2]));
        return Math.max(...dp);
    }