Domain 1EasyCORE

Richest Customer Wealth

The problem asks us to determine the wealth of the richest customer from a given grid of bank accounts. Each row in the grid represents a customer's accounts across different banks, and each element in the row represents the amount of money in a specific account.

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#Array

1672. Richest Customer Wealth

Model: qwen2.5-coder:14b
Topics: Array
FAANG asked: 0
Acceptance rate: 88.2%


Problem Breakdown

The problem asks us to determine the wealth of the richest customer from a given grid of bank accounts. Each row in the grid represents a customer’s accounts across different banks, and each element in the row represents the amount of money in a specific account.

Inputs:

  • accounts: A 2D list (grid) where accounts[i][j] is the amount of money the i-th customer has in the j-th bank.

Outputs:

  • An integer representing the maximum wealth among all customers.

Implicit Constraints or Edge Cases:

  • The grid can be empty (m = 0 or n = 0), though this is not explicitly stated.
  • All values in the grid are positive integers (1 <= accounts[i][j] <= 100).

Intuition

The brute-force approach would involve iterating through each customer’s accounts, summing up their wealth, and keeping track of the maximum wealth encountered. This approach is straightforward but can be optimized.

Brute-Force Approach:

  1. Initialize a variable max_wealth to 0.
  2. Iterate over each customer (row in the grid).
  3. For each customer, calculate their total wealth by summing up all their account values.
  4. Update max_wealth if the current customer’s wealth is greater than max_wealth.
  5. Return max_wealth.

This approach has a time complexity of O(m * n), where m is the number of customers and n is the number of bank accounts per customer.

Key Insight: The key insight is that we can optimize the brute-force approach by using Python’s built-in functions to simplify the code and potentially improve performance. Specifically, we can use the map function to sum up each customer’s wealth in a more concise manner.

Approach

  1. Use the map function to apply the sum function to each row (customer) in the grid. This will give us an iterable of total wealths for each customer.
  2. Convert this iterable to a list and use the max function to find the maximum value, which represents the wealth of the richest customer.
  3. Return the result.

This approach leverages Python’s functional programming capabilities to make the code more concise and potentially faster by reducing explicit loops.

Complexity Analysis

  • Time Complexity: O(m * n), where m is the number of customers and n is the number of bank accounts per customer. This is because we still need to iterate through each element in the grid to calculate the sum for each customer.
  • Space Complexity: O(m), as we create a list of sums for each customer.

Solution

def maximumWealth(accounts):
    # Handle the edge case where the accounts list is empty
    if not accounts:
        return 0
    
    # Calculate the wealth of each customer and find the maximum
    return max(map(sum, accounts))

Edge Cases Handled

  • Empty Input: If accounts is an empty list (either m = 0 or n = 0), the function now returns 0 immediately.
  • Single Customer: If there’s only one customer, the code will correctly return their total wealth as the maximum.
  • All Customers Have Zero Wealth: The code assumes all values are positive, so this case doesn’t apply. However, if we were to handle zero or negative values, the max function would still work correctly by returning the highest non-negative sum.