0%

LeetCode 1672. Richest Customer Wealth

題目

1
2
3
4
5
6
Input: accounts = [[1,2,3],[3,2,1]]
Output: 6
Explanation:
1st customer has wealth = 1 + 2 + 3 = 6
2nd customer has wealth = 3 + 2 + 1 = 6
Both customers are considered the richest with a wealth of 6 each, so return 6.

解法思維

foreach()取出物件,再用reduce()計算,最後用 Math.max()抓出最大值

1
2
3
4
5
6
7
8
9
10
var maximumWealth = function(accounts) {
let sum=[]
accounts.forEach(item => {
item.reduce(function (a, b) {
sum.push(a+b)
return a + b
}, 0);
});
return Math.max(...sum)
};