easy

Richest Customer Wealth

Find the customer with the most total money across accounts.

1. Define the problem

Richest Customer Wealth

You are given an m x n integer grid accounts where accountsi[j] is the amount of money the i-th customer has in the j-th bank. Return the wealth that the richest customer has, where a customer wealth is the sum of all their bank accounts . Sum each row, then track the maximum row sum seen so far as you go.

Constraints

  • m == accounts.length
  • n == accountsi.length
  • 1 ≤ m, n ≤ 50
  • 1 ≤ accountsi[j] ≤ 100

Example

Inputaccounts = [[1, 2, 3], [3, 2, 1]]
Output6

Explanation Customer 0 has 1+2+3=6, customer 1 has 3+2+1=6, so the richest wealth is 6.

2. Visualize the solution

Sum each customer row, track the largest total

Sum each customer row, track the largest total
Statusinit

Customer 0: [1, 2, 3] sums to 6. maxWealth=6.

What happens in this step

row 0 = [1, 2, 3]
sum = 1 + 2 + 3 = 6
maxWealth = 6
Step 1 of 3

Steps to visualize

  1. For each customer (each row), add up every bank balance in that row.
  2. Compare that row sum against the largest sum found so far.
  3. Keep the larger of the two as the new maximum.
  4. After checking every customer, the maximum is the richest wealth.
3. Walk through the code

Walk through the code

Same walkthrough, now with the code. Press Next to move one step and watch which lines run.

Sum each customer row, track the largest total
Statusinit

Customer 0: [1, 2, 3] sums to 6. maxWealth=6.

What happens in this step

row 0 = [1, 2, 3]
sum = 1 + 2 + 3 = 6
maxWealth = 6
Step 1 of 3
4. Solution

Solution

solution.tsTypeScript
function maximumWealth(accounts) {
  let maxWealth = 0;

  for (const row of accounts) {
    let sum = 0;
    for (const balance of row) {
      sum += balance;
    }
    maxWealth = Math.max(maxWealth, sum);
  }

  return maxWealth;
}
Time
O(m * n)
Space
O(1)
5. Test cases

Test cases

InputExpectedCovers
accounts = [[1, 2, 3], [3, 2, 1]]6example from the docstring
accounts = [[1, 5], [7, 3], [3, 5]]10one customer clearly richer than the others
accounts = [[2, 8, 7]]17smallest valid input, a single customer
accounts = [[5], [10], [3]]10each customer has only one bank account
accounts = [[4, 4], [4, 4]]8every customer has identical wealth
accounts = [[100]]100smallest possible grid, one customer with one bank