Richest Customer Wealth
Easy · Matrix · Array · Matrix
Problem
You are given an m x n integer grid accounts, where accounts[i][j] is the amount customer i holds at bank j. A customer's wealth is the total across all of their bank accounts.
Return the wealth of the richest customer.
Examples
Input: accounts = [[2,7,1],[4,4,4]] Output: 12
The customers hold 10 and 12, so the answer is 12.
Input: accounts = [[5],[9],[3]] Output: 9
With a single bank, wealth is that one amount: 9 is the largest.
Input: accounts = [[1,5],[3,3],[2,4]] Output: 6
All three customers hold 6.
Constraints
- m == accounts.length, n == accounts[i].length
- 1 <= m, n <= 50
- 1 <= accounts[i][j] <= 100
Approaches
- Baseline: store every total, then sort — time O(m · n + m log m), space O(m)
Follow-up
Rows are customers and columns are banks, so the same grid answers other questions with the same loop in a different direction. The use cases find which customer is richest and which bank holds the most money.
Related problems
Start free on Cariara · Coding problems · System design studio