2022/12/20

LeetCode 1672. Richest Customer Wealth

問題:取得最大財富
解法:
1.假定財富為0
2.透過forEach走訪,並將值透過reduce計數,判斷當前財富是否大於財富,是則指派給財富
3.回傳財富結果 JavaScript
/**
 * @param {number[][]} accounts
 * @return {number}
 */
var maximumWealth = function (accounts) {
    let max = 0;
    accounts.forEach(a => {
        let sum = a.reduce((sum, n) => sum + n, 0);
        max = max >= sum ? max : sum
    });
    return max
};