DSA AnimatorREVISION VIDEOS
← Back to problems
New

Richest Customer Wealth — LeetCode 1672 video explanation

You get a grid where each row is one customer, and each cell is the money in one bank account. A customer's wealth is the sum of their row. Return the wealth of the richest customer.

Loading videos…

DSA Animator › DSA Revision › Arrays › Richest Customer Wealth

Key moments in the Richest Customer Wealth video

0:04Problem0:15Example0:21Brute force0:30Key idea0:35Dry run0:51Code1:02Takeaway

What you will learn

Remember: row sums plus a running maximum. In a 2D grid, the outer loop picks the row, and the inner loop walks its columns. The video solves Richest Customer Wealth (LeetCode 1672, Easy) with the 2D Array pattern from the Arrays topic, and you can replay every step in the interactive Richest Customer Wealth animation.

Richest Customer Wealth video transcript

Problem

You get a grid where each row is one customer, and each cell is the money in one bank account. A customer's wealth is the sum of their row. Return the wealth of the richest customer.

Example

For rows 1 and 5, 7 and 3, and 3 and 5, the wealths are 6, 10 and 8. The richest has 10.

Brute force

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.

Key idea

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

Dry run

Start with best equal to 0. Row 0 is 1 plus 5, which is 6, so best becomes 6. Row 1 is 7 plus 3, which is 10, so best becomes 10. Row 2 is 3 plus 5, which is 8, which is less than 10. The richest customer has 10.

Code

In code, keep a best of 0. For each row, add up its accounts. Then update best with the larger value. Every cell is read once, so it's O of m times n time, and O of 1 space.

Takeaway

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

More Arrays revision videos

← All 185 DSA revision videos