Richest Customer Wealth (LeetCode 1672): Solution and Explanation
A grid accounts lists how much money each customer (a row) holds in each bank (a column). A customer's wealth is the total of their row. Return the largest wealth among all customers.
Intuition
Everything about one customer lives in one row, so their wealth is simply that row's sum. The question becomes: which row has the largest sum?
You do not need to remember every sum. Walk the rows one at a time, total the current row, and compare it with the best total seen so far. If it is larger, it becomes the new best. After the last row, the best total is the answer.
Every cell has to be read at least once, because any single bank balance could change which customer is richest. So reading each cell exactly once is already optimal.
Approach: Row sums with a running maximum
- Set
best = 0. All balances are at least 1, so any real customer beats it. - For each customer row, set
wealth = 0and add every value in the row. - After the row, set
best = max(best, wealth). - Return
bestonce every row has been totalled.
Step-by-step example
accounts = [[1,2,3],[3,2,1]]
- best starts at 0.
- Customer 0: wealth goes 0 + 1 = 1, then 1 + 2 = 3, then 3 + 3 = 6. 6 beats 0, so best = 6.
- Customer 1: wealth goes 0 + 3 = 3, then 3 + 2 = 5, then 5 + 1 = 6. 6 does not beat 6, so best stays 6.
- All customers are done, so the answer is 6.
Code: Python, Java, C++
1class Solution:
2 def maximumWealth(self, accounts: List[List[int]]) -> int:
3 best = 0
4 for customer in accounts:
5 wealth = 0
6 for money in customer:
7 wealth += money
8 best = max(best, wealth)
9 return best1class Solution {
2 public int maximumWealth(int[][] accounts) {
3 int best = 0;
4 for (int[] customer : accounts) {
5 int wealth = 0;
6 for (int money : customer) {
7 wealth += money;
8 }
9 best = Math.max(best, wealth);
10 }
11 return best;
12 }
13}1class Solution {
2public:
3 int maximumWealth(vector<vector<int>>& accounts) {
4 int best = 0;
5 for (auto& customer : accounts) {
6 int wealth = 0;
7 for (int money : customer) {
8 wealth += money;
9 }
10 best = max(best, wealth);
11 }
12 return best;
13 }
14};Time and space complexity
O(m * n)
Each of the m rows has n cells and each cell is added once.
O(1)
Only the running wealth and the best total are stored.
Other approaches
Built-in sum and max (time O(m * n), space O(1))
In Python, max(map(sum, accounts)) does the same work in one line. It is clear and just as fast, but hides the loop that interviewers often want to see.
Store every row sum, then take the max (time O(m * n), space O(m))
Build a list of row sums and return its maximum. It is easy to debug because you can print every customer's wealth, but it uses O(m) extra memory for no benefit.
Common mistakes
- Forgetting to reset
wealthto 0 for each customer, which carries one customer's money into the next total. - Summing columns instead of rows. Columns are banks, not customers.
- Returning the index of the richest customer instead of their wealth.
- Comparing inside the inner loop with a partial sum. It still gives the right answer here because balances are positive, but it does extra work and is easy to get wrong in variants with negative values.
Frequently asked questions
What if two customers have the same wealth?
The problem asks only for the wealth value, not which customer has it, so ties do not matter. Example 1 has two customers with 6 and the answer is 6.
Is starting best at 0 safe?
Yes. Every balance is at least 1 and every customer has at least one bank, so every wealth is at least 1 and the first customer always replaces 0.
What is the time complexity of Richest Customer Wealth?
O(m * n), because every cell of the grid is read once. Extra space is O(1).