Skip to content
← All posts

Richest Customer Wealth: Matrix Row Sum Pattern

5 min read
leetcodeproblemeasyarraysmatrix

You are given an m x n integer grid accounts where accounts[i][j] is the amount of money the i-th customer has in the j-th bank. Return the wealth of the richest customer, where a customer's wealth is the sum of all their bank accounts.

This is LeetCode 1672: Richest Customer Wealth.

Bank 0Bank 1Bank 2SumCustomer 01236Customer 13216max
accounts = [[1,2,3],[3,2,1]]. Sum each row to get each customer's wealth (6 and 6), then return the maximum: 6.

Why this problem matters

Richest Customer Wealth is one of those problems that looks trivial at first glance. Sum some rows, take the max. But the reason it shows up so often in beginner problem sets is that it drills a fundamental operation: traversing a 2D matrix by rows. Nearly every matrix problem you will encounter later, from spiral order traversal to dynamic programming on grids, requires you to be completely comfortable iterating through rows and columns. If this feels like muscle memory, you will move faster on harder problems.

The row aggregation pattern also appears in real-world data processing. Think of a spreadsheet where each row is a record and you need to compute a summary statistic per row. Databases do this with GROUP BY. In code, it is a nested loop. The structure is identical whether you are summing bank accounts or computing averages across sensor readings. Getting fluent with this pattern pays off well beyond LeetCode.

The key insight

Each customer's wealth is just the sum of one row. You do not need to sort, search, or compare elements against each other. You iterate through each row, sum its values, and track the maximum sum you have seen. One pass through the matrix, and you are done.

The solution

def maximumWealth(accounts):
    max_wealth = 0
    for customer in accounts:
        wealth = sum(customer)
        if wealth > max_wealth:
            max_wealth = wealth
    return max_wealth

The outer loop iterates over each row (customer). For each row, Python's built-in sum() adds up all the bank account values. We compare that total to our running maximum and update if it is larger. After processing every row, max_wealth holds the answer.

You can also write this as a one-liner using max() with a generator expression:

def maximumWealth(accounts):
    return max(sum(row) for row in accounts)

Both versions have the same time and space complexity. The explicit loop version is better for interviews because it shows your reasoning step by step.

Whenever a problem asks you to find "the maximum of some per-row aggregate," the pattern is always the same: iterate rows, compute the aggregate, track the max. This same skeleton works for finding the row with the maximum average, the row with the most even numbers, or any other row-level metric.

Visual walkthrough

Let's trace through a 3x3 matrix to see each step clearly. Our accounts are [[1,5,3],[7,3,2],[4,1,1]].

Step 1: Look at the accounts matrix

Bank 0Bank 1Bank 2SumMaxCust 0153Cust 1732Cust 2411

We have 3 customers, each with 3 bank accounts. We need to find who has the most total money.

Step 2: Sum row 0. Customer 0 wealth = 1 + 5 + 3 = 9

Bank 0Bank 1Bank 2SumMaxCust 01539Cust 1732Cust 24119

Customer 0 has a total wealth of 9. This is our max so far.

Step 3: Sum row 1. Customer 1 wealth = 7 + 3 + 2 = 12

Bank 0Bank 1Bank 2SumMaxCust 01539Cust 173212Cust 241112

Customer 1 has a total wealth of 12. That is greater than 9, so update max to 12.

Step 4: Sum row 2. Customer 2 wealth = 4 + 1 + 1 = 6

Bank 0Bank 1Bank 2SumMaxCust 01539Cust 173212Cust 2411612

Customer 2 has a total wealth of 6. That is less than 12, so max stays at 12. The richest customer has wealth 12.

After checking all three rows, the maximum wealth is 12, belonging to Customer 1. The algorithm processes each element exactly once and uses only a constant amount of extra space for the running sum and maximum.

Complexity analysis

ApproachTimeSpace
Row sum + maxO(m * n)O(1)

We visit every cell in the m x n matrix exactly once, giving us O(m * n) time. We only store two variables (max_wealth and wealth), so extra space is O(1). The input matrix itself takes O(m * n) space, but we do not create any new data structures.

The building blocks

1. Row aggregation

Iterating through a single row and reducing it to a single value is a pattern you will reuse constantly:

row_sum = 0
for val in row:
    row_sum += val

This is the same operation whether you are summing values, counting elements that match a condition, or finding the min/max within a row. The structure does not change. Only the aggregation function changes.

2. Running maximum

Tracking the best value seen so far as you iterate through a sequence:

best = 0
for candidate in values:
    if candidate > best:
        best = candidate

This pattern appears in problems like Best Time to Buy and Sell Stock, Maximum Subarray, and dozens of others. You initialize a variable to the smallest possible answer, then update it each time you find something better. Here, the "values" are the row sums you computed in the previous building block.

Edge cases

  • Single customer, single bank: accounts = [[5]]. One row, one column. The wealth is just 5.
  • All accounts are zero: accounts = [[0,0],[0,0]]. Every row sums to 0. The answer is 0.
  • One customer with many banks: accounts = [[1,2,3,4,5]]. Only one row to sum. The max is trivially that sum.
  • Many customers, one bank each: accounts = [[3],[7],[2]]. Each row has one element, so the problem reduces to finding the max in a flat list.
  • Tied wealth: accounts = [[1,2,3],[3,2,1]]. Both customers have wealth 6. The problem asks for the wealth value, not which customer, so ties do not matter.

From understanding to recall

You have read the solution and it makes sense. Sum each row, track the max. Two building blocks, a few lines of code. Clean and simple.

But simple does not mean it sticks automatically. In an interview, you might fumble with the loop structure, forget to initialize max_wealth, or overcomplicate things by trying to sort the matrix. These mistakes are not about understanding the problem. They are about not having the pattern wired into your hands.

Spaced repetition fixes this. You practice writing the row aggregation loop and the running maximum tracker from scratch at increasing intervals. After a few rounds, you see "find the max row sum" and the code just flows out. No hesitation, no second-guessing. That is the difference between knowing a pattern and owning it.

Related posts

CodeBricks breaks the richest customer wealth LeetCode problem into its row aggregation and running maximum building blocks, then drills them independently with spaced repetition. You type each piece from scratch until the pattern is automatic. When a matrix traversal question shows up in your interview, you do not think about it. You just write it.