DSA AnimatorDSA animations
Richest Customer Wealth LC #1672 Easy Array · Matrix
Problem

You are given an m×n integer grid accounts where accounts[i][j] is the amount the i-th customer has in the j-th bank. Return the wealth of the richest customer (max row sum).

Example 1
Input: accounts = [[1,2,3],[3,2,1]]
Output: 6
Explanation: Both customers have wealth 6.
Constraints: 1≤m,n≤50  |  1≤accounts[i][j]≤100
Try Examples
Custom:
Approach
Row Sum + Track Maximum
For each customer (row), sum all their bank accounts. Track the running maximum. The answer is the largest row sum seen across all customers.
Accounts Matrix
Variables
Customer i
—
Account j
—
Row Sum
—
maxWealth
0
Step Logic
Press ▶ Play or Next Step to begin.
🎉
Ready
0 / 0
Select an example above and press Play.
Algorithm
1
Init maxWealth = 0
2
For each customer row: compute rowSum
3
Update maxWealth = max(maxWealth, rowSum)
4
Return maxWealth
Time
O(m·n)
Space
O(1)
Why It Works

A customer's wealth = sum of all their bank accounts (one matrix row). We simply compute each row sum and track the maximum — no sorting or extra space needed. One nested loop covers all m×n cells.

How to solve Richest Customer Wealth

LeetCode 1672 · Easy · Arrays · Pattern: 2D Array

Why the obvious approach is slow

There's no trick to avoid here: every account must be counted once. The only thing to get right is summing each row, then keeping the maximum.

The key idea

Go row by row. Sum the row, and compare it with the best wealth seen so far.

Remember

Row sums plus a running maximum. In a 2D grid, the outer loop picks the row, and the inner loop walks its columns.

Run the animation above on your own input, or watch the 1:11 video explanation of Richest Customer Wealth with a dry run and Java code.

More Arrays problems