Richest Customer Wealth (LeetCode 1672): Solution and Explanation

Easy
Array
Matrix

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

  1. Set best = 0. All balances are at least 1, so any real customer beats it.
  2. For each customer row, set wealth = 0 and add every value in the row.
  3. After the row, set best = max(best, wealth).
  4. Return best once every row has been totalled.

Step-by-step example

accounts = [[1,2,3],[3,2,1]]
  1. best starts at 0.
  2. Customer 0: wealth goes 0 + 1 = 1, then 1 + 2 = 3, then 3 + 3 = 6. 6 beats 0, so best = 6.
  3. Customer 1: wealth goes 0 + 3 = 3, then 3 + 2 = 5, then 5 + 1 = 6. 6 does not beat 6, so best stays 6.
  4. 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 best
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(m * n)

Each of the m rows has n cells and each cell is added once.

Space

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 wealth to 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).