來刷 LeetCode 吧! 02 1672. Richest Customer Wealth
LeetCode
分析題目
傳入一個 array,裡面各自都是一個 array。
找出相加最大的那一個。
ex:
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.
第一直覺
跟上一題有點像,直接修改 arrray,之後找出最大的那個就好。
- 首先遍歷 “accounts”
- 將 “accounts [ i ]” 內的 array 做加總。
var maximumWealth = function (accounts) { for (let i = 0; i < accounts.length; i++) { accounts[i] = accounts[i].reduce((a, b) => a + b, 0); } return Math.max(...accounts); };
這應該是最佳寫法了吧😂