← Static tasks

1672. Richest Customer Wealth

leetcode easy

#array#csharp#easy#leetcode#math

Task

Вам дан целочисленный массив размером m x n под названием accounts, где accounts[i][j] — это сумма денег, которую i-й клиент имеет в j-м банке. Верните богатство самого богатого клиента.

Богатство клиента — это сумма денег, которую он имеет во всех своих банковских счетах. Самый богатый клиент — это клиент, который имеет максимальное богатство.

Пример:

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.

C# solution

matched/original
public class Solution {
    public int MaximumWealth(int[][] accounts) {
        int maxWealthSoFar = 0;
        
        foreach (var account in accounts) {
            int currCustomerWealth = account.Sum();
            maxWealthSoFar = Math.Max(maxWealthSoFar, currCustomerWealth);
        }
        
        return maxWealthSoFar;
    }
}

C++ solution

auto-draft, review before submit
#include <bits/stdc++.h>
using namespace std;

// Auto-generated C++ draft from the C# solution. Review containers, LINQ and helper types before submit.
class Solution {
public:
    public int MaximumWealth(int[][] accounts) {
        int maxWealthSoFar = 0;
        
        foreach (var account in accounts) {
            int currCustomerWealth = account.Sum();
            maxWealthSoFar = max(maxWealthSoFar, currCustomerWealth);
        }
        
        return maxWealthSoFar;
    }
}

Java solution

matched/original
class Solution {
    public int maximumWealth(int[][] accounts) {
        int maxWealthSoFar = 0;
        
        for (int[] account : accounts) {
            int currCustomerWealth = 0;
            for (int money : account) {
                currCustomerWealth += money;
            }
            maxWealthSoFar = Math.max(maxWealthSoFar, currCustomerWealth);
        }
        
        return maxWealthSoFar;
    }
}

JavaScript solution

matched/original
var maximumWealth = function(accounts) {
    let maxWealthSoFar = 0;
    
    for (let account of accounts) {
        let currCustomerWealth = account.reduce((acc, money) => acc + money, 0);
        maxWealthSoFar = Math.max(maxWealthSoFar, currCustomerWealth);
    }
    
    return maxWealthSoFar;
};

Python solution

matched/original
class Solution:
    def maximumWealth(self, accounts: List[List[int]]) -> int:
        maxWealthSoFar = 0
        
        for account in accounts:
            currCustomerWealth = sum(account)
            maxWealthSoFar = max(maxWealthSoFar, currCustomerWealth)
        
        return maxWealthSoFar

Explanation

Algorithm

1⃣Пройдите по всем клиентам в массиве accounts.

2⃣Для каждого клиента вычислите сумму денег на всех его банковских счетах и сравните её с максимальным богатством, найденным до этого момента.

3⃣Если текущее богатство больше максимального, обновите максимальное значение. Верните максимальное богатство.

😎