Greatest Sum Divisible by Three LeetCode solution in JavaScript
1var maxSumDivThree = function (nums) {2// there are 3 options for how the sum fit's into 3 via mod % 33// track those 3 options via indices in the dp array4// dp[0] = %3 === 05// dp[1] = %3 === 16// dp[2] = %3 === 27let dp = new Array(3).fill(0);8for (let num of nums) {9for (let i of dp.slice(0)) {10let sum = i + num;11let mod = sum % 3;12// on each pass, set the value of dp[mod] to the Math.max val13dp[mod] = Math.max(dp[mod], sum);14}15}16return dp[0];17};