Richest Customer Wealth

You are given an m x n integer grid accounts where accounts[i][j] is the amount of money the i​​​​​​​​​​​th​​​​ the customer has in the j​​​​​​​​​​​th​​​​ bank. Return the wealth that the richest customer has.
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

Approach

Java

public class RichestCustomerWealth {
    public static void main(String[] args) {
        int[][] accounts = { { 123 }, { 321 } };
        System.out.println(maximumWealth(accounts));
    }

    static int maximumWealth(int[][] accounts) {
        int ans = Integer.MIN_VALUE;
        for (int i = 0; i < accounts.length; i++) {
            int sum = 0;
            for (int j = 0; j < accounts[i].length; j++)
                sum += accounts[i][j];
            ans = Math.max(ans, sum);
        }
        return ans;
    }
}

C++

#include <bits/stdc++.h>
using namespace std;

int maximumWealth(vector<vector<int>> &accounts)
{
    int ans = INT_MIN;
    for (int i = 0i < accounts.size(); i++)
    {
        int sum = 0;
        for (int j = 0j < accounts[i].size(); j++)
            sum += accounts[i][j];
        ans = max(anssum);
    }
    return ans;
}

int main()
{
    vector<vector<int>> accounts = {{123}, {321}};
    cout << maximumWealth(accounts);
    return 0;
}


No comments:

Post a Comment