Richest Customer Wealth

Easy
115.

Richest Customer Wealth

Richest Customer Wealth is an easy coding problem on arrays and matrix.

accounts[i][j] is the money the i-th customer holds in the j-th bank. A customer's wealth is the sum of their accounts.

Return the wealth of the richest customer.

Example 1

Input
accounts = [[1,2,3],[3,2,1]]
Output
6

Example 2

Input
accounts = [[1,5],[7,3],[3,5]]
Output
10
Explanation
Customer 2: 7 + 3 = 10.

Constraints

  • 1 <= customers, banks <= 8
  • 1 <= accounts[i][j] <= 100
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {number[][]} accounts
 * @return {number}
 */
var maximumWealth = function(accounts) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Richest Customer Wealth — Solution in Python, Java & C++

Richest Customer Wealth solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.

Richest Customer Wealth is an easy coding problem on arrays and matrix.

  • Difficulty: Easy
  • Topics: Arrays, Matrix
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

accounts[i][j] is the money the i-th customer holds in the j-th bank. A customer's wealth is the sum of their accounts.

Return the wealth of the richest customer.

Example 1

Input: accounts = [[1,2,3],[3,2,1]]
Output: 6

Example 2

Input: accounts = [[1,5],[7,3],[3,5]]
Output: 10
Explanation: Customer 2: 7 + 3 = 10.

Constraints

  • 1 <= customers, banks <= 8
  • 1 <= accounts[i][j] <= 100

Hints for Richest Customer Wealth

  1. Hint 1

    Sum each row.

  2. Hint 2

    Track the running maximum.

How to solve Richest Customer Wealth

Each customer's wealth is just the sum of their row, so the answer is the largest row sum. One pass over the matrix, keeping a running maximum — no sorting, no extra storage.

Approach

  1. Start the best wealth at zero.
  2. For each row, add up its entries.
  3. If that total beats the current best, keep it.
  4. Return the best.

Why it works

There is no structure to exploit here — every account must be read to know its customer's total, so O(rows · cols) is optimal. Tracking a running maximum rather than collecting all the sums keeps the extra space constant.

Complexity

  • Time — O(rows · cols)
  • Space — O(1)

Pitfalls

  • Reset the row sum for each customer; carrying it over accumulates across rows.
  • The answer is the maximum row sum, not the total of the whole matrix.
  • All values are positive here, so seeding the best at zero is safe — with possible negatives you would seed from the first row instead.

Reference solution

Python

from typing import List

def maximumWealth(accounts: List[List[int]]) -> int:
    return max(sum(a) for a in accounts)

Java

public static int maximumWealth(int[][] accounts) {
    int best = 0;
    for (int[] row : accounts) {
        int sum = 0;
        for (int v : row) sum += v;
        if (sum > best) best = sum;
    }
    return best;
}

C++

int maximumWealth(vector<vector<int>>& accounts) {
    int best = 0;
    for (const auto& row : accounts) {
        int sum = 0;
        for (int v : row) sum += v;
        if (sum > best) best = sum;
    }
    return best;
}

JavaScript

var maximumWealth = function(accounts) {
    let best = 0;
    for (const a of accounts) {
        let sum = 0;
        for (const x of a) sum += x;
        best = Math.max(best, sum);
    }
    return best;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 1199 arrays problems · the whole catalogue

Learn the technique: Arrays · Matrix and Grid Traversal