Leetcode - Richest Customer Wealth

wanderer-s·2022년 1월 31일
0

Algorithm

목록 보기
15/19
post-thumbnail

Problem

Richest Customer Wealth

You are given an m x n integer grid accounts where accounts[i][j] is the amount of money the ith customer has in the jth bank. Return the wealth that the richest customer has.

m x n 형태의 정수로 이루어진 2중배열 accounts이 주어졌을 때, accounts[i][j]ith 손님이 jth 은행에 가지고 있는 돈의 액수다
가장 돈이 많은 손님의 재산의 금액을 구하시오

A customer's wealth is the amount of money they have in all their bank accounts. The richest customer is the customer that has the maximum wealth.

손님의 재산은 은행에 가지고 있는 모든 돈의 양이며, 가장 돈이 많은 손님은 재산이 가장 많은 손님이다. (굳이 이러한 설명이..??)

Example 1:

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.

Example 2:

Input: accounts = [[1,5],[7,3],[3,5]]
Output: 10
Explanation: 
1st customer has wealth = 6
2nd customer has wealth = 10 
3rd customer has wealth = 8
The 2nd customer is the richest with a wealth of 10.

Example 3:

Input: accounts = [[2,8,7],[7,1,3],[1,9,5]]
Output: 17

Constraints:

  • m == accounts.length
  • n == accounts[i].length
  • 1 <= m, n <= 50
  • 1 <= accounts[i][j] <= 100

Solution

JavaScript

  1. 첫번째 시도
/**
 * @param {number[][]} accounts
 * @return {number}
 */
var maximumWealth = function(accounts) {
    let maxCount = 0
    accounts.forEach(account => {
        let sum = 0
        account.forEach((money) => {
            sum += money
        })
        maxCount = Math.max(maxCount, sum)
    })
    return maxCount
};
  1. 두번째 시도 (더 간편히 할 수 있을거 같아서)
var maximumWealth = function(accounts) {
  return Math.max(...accounts.map(account => {
    return account.reduce((sum, money) => sum + money, 0)
  }))
};

Feedback은 언제나 환영입니다🤗

profile
You only get one life. It's actually your duty to live it as fully as possible.

0개의 댓글