Richest Customer Wealth solution and explanation
9 more videos on this problem
Leetcode Solution - 1672 Richest Customer WealthJohn Leonardo · 4:07 · 2K views
LeetCode - Richest Customer Wealth in PythonKnowledge Mavens · 7:34 · 2K views
1672. Richest Customer Wealth (Leetcode Easy)Programming Live with Larry · 1:57 · 2K views
1672. Richest Customer Wealth | Interview Preparation | English | code iocode io - English · 8:59 · 1K views
Richest Customer Wealth - LeetCode 1672 - PythonGordon Zhu · 6:44 · 1K views
How to Solve 1672 Richest Customer Wealth (LeetCode Javascript)Becoming A Better Dev (with David) · 6:25 · 1K views
LEETCODE SOLUTION 1672 | Richest Customer Wealth | Leetcode for BEGINNERS | EASY | JavascriptCodingForBeginners · 7:58 · 966 views
Richest Customer Wealth | LeetCode 1672 | EnglishCodeClips with Abhishek Ranjan · 5:54 · 840 views
LeetCode 1672 : Richest Customer Wealth | Solution in JAVAAmit Manjarly · 8:02 · 728 views
Richest Customer Wealth LeetCodeLast Programmer · 3:40 · 594 views
The problem
You are given an m x n integer grid accounts where accounts[i][j] is the amount of money the ith customer has in the jth bank. Return the wealth that the richest customer has.
A customer's wealth is the amount of money they have in all their bank accounts. The richest customer is the customer that has the maximum wealth.
Example 1:
Input: accounts = [[1,2,3],[3,2,1]] Output: 6 Explanation:1st customer has wealth = 1 + 2 + 3 = 62nd customer has wealth = 3 + 2 + 1 = 6Both customers are considered the richest with a wealth of 6 each, so return 6.
Example 2:
Input: accounts = [[1,5],[7,3],[3,5]] Output: 10 Explanation: 1st customer has wealth = 6 2nd customer has wealth = 10 3rd customer has wealth = 8 The 2nd customer is the richest with a wealth of 10.
Example 3:
Input: accounts = [[2,8,7],[7,1,3],[1,9,5]] Output: 17
Constraints:
m == accounts.lengthn == accounts[i].length1 <= m, n <= 501 <= accounts[i][j] <= 100
Approach overview
Problem Overview: You receive a 2D matrix where accounts[i][j] represents the money the i-th customer has in the j-th bank. The task is to compute each customer's total wealth (sum of their row) and return the maximum wealth among all customers.
Approach 1: Iterative Calculation (O(m*n) time, O(1) space)
The most direct solution iterates through every customer row and calculates the sum of their accounts. For each row in the matrix, accumulate values using a simple loop and track the maximum sum encountered so far. This works because each row represents one customer's wealth distribution across banks. After computing the row total, compare it with the current maximum and update if necessary. The algorithm scans every cell exactly once, giving O(m*n) time complexity where m is the number of customers and n is the number of banks. Since only a few variables are used for tracking totals, the space complexity remains O(1). This approach relies on basic traversal of a array and is the most common interview implementation.
Approach 2: Matrix Map-Reduce Pattern (O(m*n) time, O(m) space)
Languages with strong functional utilities allow a compact solution using a map-reduce style pipeline. First compute the sum of each row using operations like sum() or reduce(). This produces an intermediate list where each value represents a customer's total wealth. Then apply a max() operation to find the richest customer. Conceptually, this is a two-step process: map each row to its total wealth, then reduce the results to the largest value. The time complexity remains O(m*n) because every element of the matrix must still be processed. Space complexity becomes O(m) if the intermediate wealth list is stored. This style is common in Python and JavaScript and demonstrates familiarity with functional transformations on collections.
Recommended for interviews: The iterative row-sum approach is what interviewers typically expect. It clearly shows you understand how to traverse a 2D array and maintain running aggregates. The map-reduce variant is concise and expressive in languages that support it well, but interviews usually prioritize the explicit loop implementation because it demonstrates control over iteration and memory usage.
Approach 1: Iterative Calculation Approach
This approach involves iterating over each customer's accounts, calculating their total wealth by summing up the amounts, and keeping track of the maximum wealth found during these calculations. This method is simple and direct, leveraging basic iteration and comparison.
def maximumWealth(accounts):
max_wealth = 0
for customer in accounts:
current_wealth = sum(customer)
max_wealth = max(max_wealth, current_wealth)
return max_wealth
print(maximumWealth([[2,8,7],[7,1,3],[1,9,5]]))public class RichestCustomerWealth {
public int maximumWealth(int[][] accounts) {
int maxWealth = 0;
for (int[] customer : accounts) {
int currentWealth = 0;
for (int money : customer) {
currentWealth += money;
}
maxWealth = Math.max(maxWealth, currentWealth);
}
return maxWealth;
}
public static void main(String[] args) {
RichestCustomerWealth solver = new RichestCustomerWealth();
int[][] accounts = {{2,8,7},{7,1,3},{1,9,5}};
System.out.println(solver.maximumWealth(accounts));
}
}#include <vector>
#include <iostream>
int maximumWealth(std::vector<std::vector<int>>& accounts) {
int maxWealth = 0;
for (const auto& customer : accounts) {
int currentWealth = 0;
for (int money : customer) {
currentWealth += money;
}
maxWealth = std::max(maxWealth, currentWealth);
}
return maxWealth;
}
int main() {
std::vector<std::vector<int>> accounts = {{2,8,7},{7,1,3},{1,9,5}};
std::cout << maximumWealth(accounts) << std::endl;
return 0;
}function maximumWealth(accounts) {
let maxWealth = 0;
for (const customer of accounts) {
const currentWealth = customer.reduce((a, b) => a + b, 0);
maxWealth = Math.max(maxWealth, currentWealth);
}
return maxWealth;
}
console.log(maximumWealth([[2,8,7],[7,1,3],[1,9,5]]));#include <stdio.h>
int maximumWealth(int accounts[50][50], int m, int n) {
int maxWealth = 0;
for (int i = 0; i < m; ++i) {
int currentWealth = 0;
for (int j = 0; j < n; ++j) {
currentWealth += accounts[i][j];
}
if (currentWealth > maxWealth) {
maxWealth = currentWealth;
}
}
return maxWealth;
}
int main() {
int accounts[3][3] = {{2,8,7},{7,1,3},{1,9,5}};
printf("%d\n", maximumWealth(accounts, 3, 3));
return 0;
}using System;
public class Solution {
public int MaximumWealth(int[][] accounts) {
int maxWealth = 0;
foreach (var customer in accounts) {
int currentWealth = 0;
foreach (var money in customer) {
currentWealth += money;
}
maxWealth = Math.Max(maxWealth, currentWealth);
}
return maxWealth;
}
public static void Main() {
int[][] accounts = new int[][] { new int[] {2,8,7}, new int[] {7,1,3}, new int[] {1,9,5} };
Solution sol = new Solution();
Console.WriteLine(sol.MaximumWealth(accounts));
}
}Uses Python's built-in sum function to efficiently handle the sum calculation for each customer, comparing it to track the largest.
Time Complexity: O(m * n)
Space Complexity: O(1)
Approach 2: Matrix Utilization via Map-Reduce
This approach considers a more functional programming style by mapping over the initial list to compute each customer's wealth, thereby generating an array of wealth values, which is in turn reduced (or simply searched) to obtain the maximum wealth.
def maximumWealth(accounts):
return max(map(sum, accounts))
print(maximumWealth([[2,8,7],[7,1,3],[1,9,5]]))import java.util.stream.*;
public class RichestCustomerWealth {
public int maximumWealth(int[][] accounts) {
return Arrays.stream(accounts)
.mapToInt(customer -> IntStream.of(customer).sum())
.max()
.orElse(0);
}
public static void main(String[] args) {
RichestCustomerWealth solver = new RichestCustomerWealth();
int[][] accounts = {{2,8,7},{7,1,3},{1,9,5}};
System.out.println(solver.maximumWealth(accounts));
}
}function maximumWealth(accounts) {
return Math.max(...accounts.map(customer => customer.reduce((a, b) => a + b, 0)));
}
console.log(maximumWealth([[2,8,7],[7,1,3],[1,9,5]]));Here, map is used to apply the sum function across all customers in accounts, and then max finds the highest resulting sum.
Time Complexity: O(m * n)
Space Complexity: O(m)
Approach 3: Summation
We traverse accounts and find the maximum sum of each row.
The time complexity is O(m times n), where m and n are the number of rows and columns in the grid, respectively. The space complexity is O(1).
function maximumWealth(accounts: number[][]): number {
return accounts.reduce(
(r, v) =>
Math.max(
r,
v.reduce((r, v) => r + v),
),
0,
);
}func maximumWealth(accounts [][]int) int {
ans := 0
for _, e := range accounts {
s := 0
for _, v := range e {
s += v
}
if ans < s {
ans = s
}
}
return ans
}class Solution {
fun maximumWealth(accounts: Array<IntArray>): Int {
var max = 0
for (account in accounts) {
val sum = account.sum()
if (sum > max) {
max = sum
}
}
return max
}
}impl Solution {
pub fn maximum_wealth(accounts: Vec<Vec<i32>>) -> i32 {
accounts.iter().map(|v| v.iter().sum()).max().unwrap()
}
}class Solution {
/**
* @param Integer[][] $accounts
* @return Integer
*/
function maximumWealth($accounts) {
$rs = 0;
for ($i = 0; $i < count($accounts); $i++) {
$sum = 0;
for ($j = 0; $j < count($accounts[$i]); $j++) {
$sum += $accounts[$i][$j];
}
if ($sum > $rs) {
$rs = $sum;
}
}
return $rs;
}
}Complexity comparison
| Approach | Complexity |
|---|---|
| Iterative Calculation Approach | Time Complexity: O(m * n) |
| Matrix Utilization via Map-Reduce | Time Complexity: O(m * n) |
| Summation | – |
Complexity analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Row Summation | O(m*n) | O(1) | Best general solution; interview-friendly and memory efficient |
| Matrix Map-Reduce | O(m*n) | O(m) | Useful in Python/JavaScript for concise functional-style code |
Frequently asked questions
Is Richest Customer Wealth easy or hard?
Richest Customer Wealth is categorized as an Easy problem. It focuses on basic matrix traversal and aggregation logic, making it suitable for beginners practicing array and loop fundamentals.
Richest Customer Wealth Python/Java solution
In Python, the solution often uses max(sum(row) for row in accounts) to compute the richest customer in one line. In Java, iterate through each row with a loop, accumulate the row sum, and update a running maximum variable.
How to solve Richest Customer Wealth in O(n)?
If n represents the total number of elements in the matrix, the solution already runs in linear time O(n). Iterate through each row, compute its sum, and update the maximum wealth. Each matrix element contributes to exactly one addition operation.
What is the best approach for Richest Customer Wealth?
The best approach is to iterate through each row of the accounts matrix, compute the sum of that row, and track the maximum sum seen so far. This scans every account once, giving O(m*n) time complexity and O(1) extra space. It is simple, efficient, and the solution most interviewers expect.
Is Richest Customer Wealth asked at Google/Amazon/Meta?
Richest Customer Wealth is commonly used as a warm-up problem in coding interviews and online assessments. Companies like Amazon, Google, and Meta often include similar array or matrix traversal questions to test basic iteration and aggregation skills.
What data structure is used in Richest Customer Wealth?
The problem uses a 2D array (matrix) where rows represent customers and columns represent bank accounts. The algorithm mainly performs sequential traversal and summation across rows without requiring advanced data structures.
What is the time complexity of Richest Customer Wealth?
The time complexity is O(m*n), where m is the number of customers (rows) and n is the number of banks (columns). Every account balance must be visited at least once to compute row sums. Space complexity can be O(1) if totals are calculated on the fly.
