We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Richest Customer Wealth

Number: 1791

Difficulty: Easy

Paid? No

Companies: Google, Amazon, Adobe, Apple, Accenture


Problem Description

Given an m x n integer grid where each row represents a customer's bank accounts, determine the maximum wealth among all customers, where a customer's wealth is the sum of money in all their bank accounts.


Key Insights

  • Each row in the grid corresponds to one customer.
  • A customer's wealth is calculated by summing up the amounts in that row.
  • The problem requires finding the maximum sum among all the rows.
  • Simple iteration over the grid is sufficient without additional data structures.

Space and Time Complexity

Time Complexity: O(m * n) where m is the number of customers and n is the number of banks
Space Complexity: O(1) extra space, as we only keep track of the maximum sum


Solution

The solution involves iterating over each customer (each row in the grid) and calculating the sum of their bank account values. Maintain a variable to store the maximum wealth encountered. Since each customer's wealth is computed independently, the algorithm only requires constant additional memory.


Code Solutions

# Function to compute the richest customer's wealth
def maximumWealth(accounts):
    # Initialize maximum wealth to 0
    max_wealth = 0
    # Iterate over each customer's accounts in the grid
    for customer in accounts:
        # Calculate the sum of money for the current customer
        customer_wealth = sum(customer)
        # Update max_wealth if current customer's wealth is greater
        if customer_wealth > max_wealth:
            max_wealth = customer_wealth
    # Return the highest wealth found
    return max_wealth

# Example usage:
# accounts = [[1,2,3],[3,2,1]]
# print(maximumWealth(accounts))  # Output: 6
← Back to All Questions