Richest Customer Wealth - Problem

Imagine you're a bank analyst tasked with identifying the wealthiest customer across multiple banking institutions. You have access to a comprehensive financial report represented as a matrix where each row represents a customer and each column represents a different bank.

Given an m x n integer matrix accounts where accounts[i][j] represents the amount of money the ith customer has in the jth bank, your goal is to find the maximum total wealth among all customers.

A customer's wealth is calculated as the sum of all money they have across all their bank accounts. Return the wealth of the richest customer.

Example: If customer 0 has [1, 2, 3] in three banks, their total wealth is 1 + 2 + 3 = 6.

Input & Output

example_1.py โ€” Basic Case
$ Input: accounts = [[1,2,3],[3,2,1]]
โ€บ Output: 6
๐Ÿ’ก Note: Customer 1 has wealth = 1 + 2 + 3 = 6. Customer 2 has wealth = 3 + 2 + 1 = 6. Both have maximum wealth of 6.
example_2.py โ€” Unequal Wealth
$ Input: accounts = [[1,5],[7,3],[3,5]]
โ€บ Output: 10
๐Ÿ’ก Note: Customer 1 has wealth = 1 + 5 = 6. Customer 2 has wealth = 7 + 3 = 10. Customer 3 has wealth = 3 + 5 = 8. Maximum wealth is 10.
example_3.py โ€” Single Customer
$ Input: accounts = [[2,8,7]]
โ€บ Output: 17
๐Ÿ’ก Note: There's only one customer with wealth = 2 + 8 + 7 = 17.

Visualization

Tap to expand
Customer Wealth AnalysisBank ABank BBank CTotal WealthCustomer 1$1$2$3$6Customer 2$3$2$1$6Maximum Wealth Found$6Both customers have equal wealth, so we return 6
Understanding the Visualization
1
Start Analysis
Begin with the first customer's portfolio
2
Sum Accounts
Add up all account balances for this customer
3
Track Maximum
Compare with the highest wealth seen so far
4
Next Customer
Move to the next customer and repeat
5
Final Result
Return the highest total wealth found
Key Takeaway
๐ŸŽฏ Key Insight: We only need to track the running maximum while calculating each customer's total wealth, making this an optimal O(1) space solution.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(m ร— n)

We visit each element in the matrix exactly once, where m is customers and n is banks

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using a few variables to track current and maximum wealth

n
2n
โœ“ Linear Space

Constraints

  • m == accounts.length
  • n == accounts[i].length
  • 1 โ‰ค m, n โ‰ค 50
  • 1 โ‰ค accounts[i][j] โ‰ค 100
Asked in
Amazon 25 Google 18 Microsoft 15 Apple 12
127.5K Views
High Frequency
~8 min Avg. Time
3.2K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen