#1672 Richest Customer Wealth
the problem
In plain words
accounts[i][j] is the money customer i keeps in bank j. A customer's wealth is the total across all their banks. Return the wealth of the richest customer.
in accounts = [[1,2,3],[3,2,1]]
out 6
Both customers have 6.
in accounts = [[1,5],[7,3],[3,5]]
out 10
Wealths are 6, 10 and 8.
in accounts = [[2,8,7],[7,1,3],[1,9,5]]
out 17
- 1 ≤ m, n ≤ 50
- 1 ≤ accounts[i][j] ≤ 100
your turn
Try it before you peek
Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.
Solve it in the browser
LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.
Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.
Marking the problem solved, or opening every hint, also shows it.
the aha
Key insight
patternMatrix traversal & in-place transforms
Each row is one customer. Sum a row, compare it with the best so far, move on — accounts[r] is that customer's whole row array.
solutions
From brute force to optimal
Compute all the wealths into an array, then take its maximum.
- wealth = new int[m].
- Fill wealth[r] with row r's sum.
- Return the largest wealth[r].
Every cell is added once, then m more steps for the max.
One total per customer.
class Solution { public int maximumWealth(int[][] accounts) { int[] wealth = new int[accounts.length]; // one total per customer (row) for (int r = 0; r < accounts.length; r++) { for (int c = 0; c < accounts[r].length; c++) wealth[r] += accounts[r][c]; } int best = wealth[0]; for (int w : wealth) best = Math.max(best, w); return best; }}Keep only the best total seen so far.
- best = 0.
- For each row: wealth = sum of its cells.
- best = max(best, wealth).
Every cell is read exactly once.
Two ints: wealth and best.
class Solution { public int maximumWealth(int[][] accounts) { int best = 0; for (int r = 0; r < accounts.length; r++) { // one row = one customer int wealth = 0; for (int c = 0; c < accounts[r].length; c++) { // THIS row's length wealth += accounts[r][c]; } best = Math.max(best, wealth); } return best; }}Dry run
Map each row to its sum, then take the max.
Arrays.stream(accounts)streams the rows.mapToInt(row -> Arrays.stream(row).sum())..max().getAsInt().
Same work, plus stream overhead per row.
Streams process one row at a time.
class Solution { public int maximumWealth(int[][] accounts) { return Arrays.stream(accounts) // Stream<int[]>: one row at a time .mapToInt(row -> Arrays.stream(row).sum()) // each row -> its sum .max() .getAsInt(); // at least one customer exists }}in the room
How to say it in the interview
talk track
- Confirm there's no overflow risk: at most 50 × 100 = 5,000 per customer.
- Rows with the outer loop, cells with the inner loop, one running max.
traps
- Using
accounts[0].lengthfor every row — breaks on jagged input. - Swapping indexes:
accounts[c][r]reads the wrong customer or goes out of bounds.
what they ask next
Follow-ups
Return which customer is richest?
Track bestRow next to best and update both when a row beats the record.
before you submit
Edge cases to test
- One customer: return that row's sum.
- One bank per customer: every row has one cell.
- Rows of different lengths (not in LeetCode, but legal Java): loop with
accounts[r].length.
practise next
Similar problems
- #54Spiral MatrixMediumin Matrices
- #304Range Sum Query 2D - ImmutableMediumin Hashing