Skip to main content

< Back to Data Structures and Algorithms

LeetCode 1672 · Grids

Richest Customer Wealth

The largest total of any customer's bank accounts, each row a customer.

Step through it

Examples
Edge cases
accounts
157335

Step 0 of 6

Output

Running… play or step to the end.

The code

/**
 * Determines the richest customer out of a list of customers.
 * Each customer has a list of bank account values.
 *
 * @param {number[][]} accounts
 * @return {number}
 */
 const maximumWealth = accounts => {
    // Keeps track of the greatest customer wealth.
    let richestCustomerWealth = 0
    
    // O(n) loop over every customer.
    for (let customer in accounts) {
        // Keeps track of the customers' wealth.
        let customerWealth = 0
        
        // O(n) loop over every bank value for the customer.
        for (let bankValue of accounts[customer]) {
            // Adds the bank value to the customers' wealth.
            customerWealth += bankValue
        }
        
        // Determine if this is the new highest customer value.
        richestCustomerWealth = Math.max(richestCustomerWealth, customerWealth)
    }
    
    // Highest customer value found.
    return richestCustomerWealth
}

View it on GitHub (opens in a new tab)