1672. Total assets of the richest client (C++)
1 topic description
Give you an mxn integer grid of accounts, where accounts[i][j] is the number of assets held by the i-th customer in the j-th bank. Returns the total amount of assets owned by the richest customer.
The total assets of a customer is the sum of the assets they have in custody in various banks. The richest client is the client with the largest total assets.
2 Example description
2.1 Example 1
Input: accounts = [[1,2,3],[3,2,1]]
Output: 6
Explanation:
Total assets of the first customer = 1 + 2 + 3 = 6
Total assets of the second customer = 3 + 2 + 1 = 6
Both customers are the richest, and the total assets are both 6, so 6 is returned.
2.2 Example 2
Input: accounts = [[1,5],[7,3],[3,5]]
Output: 10
Explanation:
The total assets of the
first customer = 6 The total assets of the second customer = 10
The third Total assets of each customer = 8
The second customer is the richest, total assets of 10
2.3 Example 3
Input: accounts = [[2,8,7],[7,1,3],[1,9,5]]
Output: 17
3 Problem solving tips
m == accounts.length
n == accounts[i].length
1 <= m, n <= 50
1 <= accounts[i][j] <= 100
4 Detailed source code (C++)
class Solution {
public:
int maximumWealth(vector<vector<int>>& accounts)
{
int max = 0 ,sum = 0;
for (int i = 0 ; i < accounts.size() ; i++)
{
for (int j = 0 ; j < accounts[i].size() ; j++)
{
sum = accounts[i][j] + sum ;
}
if (sum > max)
{
max = sum ;
sum = 0;
}
else
{
sum = 0;
}
}
return max;
}
};