Richest Customer Wealth - Problem

You are given an m x n integer matrix accounts where accounts[i][j] is the amount of money the ith customer has in the jth bank.

Return the wealth of the richest customer.

A customer's wealth is the sum of money they have in all their bank accounts. The richest customer is the customer that has the maximum wealth.

Input & Output

Example 1 — Equal Wealth
$ 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 customers have equal wealth of 6.
Example 2 — Different Wealth
$ Input: accounts = [[1,5],[7,3],[3,5]]
Output: 10
💡 Note: Customer 1: 1+5 = 6, Customer 2: 7+3 = 10, Customer 3: 3+5 = 8. The richest customer has wealth = 10.
Example 3 — Single Account
$ Input: accounts = [[2,8,7],[7,1,3],[1,9,5]]
Output: 17
💡 Note: Customer 1: 2+8+7 = 17, Customer 2: 7+1+3 = 11, Customer 3: 1+9+5 = 15. Maximum wealth is 17.

Constraints

  • 1 ≤ m, n ≤ 50
  • 1 ≤ accounts[i][j] ≤ 100

Visualization

Tap to expand
Richest Customer Wealth INPUT accounts matrix (m x n) 1 2 3 3 2 1 C1 C2 B1 B2 B3 accounts = [ [1,2,3], [3,2,1]] Customer 1 Customer 2 ALGORITHM STEPS 1 Initialize maxWealth Set maxWealth = 0 2 Loop each customer Sum all bank accounts 3 Compare wealth Update if sum > maxWealth 4 Return maxWealth After all customers Calculation Process C1: 1 + 2 + 3 = 6 max = 6 C2: 3 + 2 + 1 = 6 max = 6 Final: maxWealth = 6 FINAL RESULT Maximum Wealth 6 Output: 6 Both customers have equal wealth of 6 [OK] Verified Time: O(m * n) Space: O(1) Key Insight: Single pass optimization: Instead of storing all sums, track maximum as we go. For each customer, calculate sum of all bank accounts and compare with current max. This achieves O(1) space complexity while maintaining O(m*n) time complexity. TutorialsPoint - Richest Customer Wealth | Optimized Single Pass
Asked in
LeetCode 25 Amazon 15
125.0K Views
High Frequency
~5 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