easy Problem 1 Richest Customer Wealth
Problem Statement
You are given an m x n matrix accounts where accounts[i][j] is the amount of money the ith customer has in the jth bank.
Return the wealth that the richest customer has.
Imagine every customer has multiple bank accounts, with each account holding a certain amount of money. The total wealth of a customer is calculated by summing all the money across all their bank accounts.
Examples
-
Example 1:
- Input: accounts =
[[5,2,3], [0,6,7]] - Expected Output:
13 - Justification: The total wealth of the first customer is 10 and of the second customer is 13. So, the output is 13 as it's the maximum among all customers.
- Input: accounts =
-
Example 2:
- Input: accounts =
[[1,2], [3,4], [5,6]] - Expected Output:
11 - Justification: Total wealth for each customer is [3, 7, 11]. Maximum of these is 11.
- Input: accounts =
-
Example 3:
- Input: accounts =
[[5,10,15], [10,20,30], [15,30,45]]- Expected Output:
90 - Justification: Total wealth for each customer is [30, 60, 90]. The wealthiest customer has 90.
Constraints:
m == accounts.lengthn == accounts[i].length1 <= m, n <= 501 <= accounts[i][j] <= 100
Try it yourself
Try solving this question here:
🎯 STRICT STANDOUT: Why / complexity derivation / pattern+when-not / edges / drills — Problem 1 Richest Customer Wealth (easy)
Why this exists (judgment layer)
Row-reduction max is the simplest matrix scan pattern: one pass, reduce each row, track global max — baseline before diagonal/spiral/zero-marker tricks.
Worked example & complexity derivation
accounts=[[5,2,3],[0,6,7]]
row0 sum=10; row1 sum=13; answer max=13
[[1,2],[3,4],[5,6]] → 3,7,11 → 11
Time: visit every cell once Θ(m·n); space O(1) besides input
Cannot do better asymptotically: answer depends on all entries in general
Pattern transfer & when-NOT
Pattern: ROW AGGREGATE THEN MAX (matrix as list of vectors). When-NOT: need column wealth → transpose logic; sparse accounts → skip zeros if representation sparse; streaming max without storing full matrix if rows arrive one-by-one (same algorithm). Not a graph; not DP.
Edge cases (hand-run)
m=1,n=1 → that single value. All equal rows → that sum. Constraints positive amounts 1..100 so no empty-account zeros forced, but sum still works with zeros.
Hostile-panel drills (defend the decision)
Q1. Why Θ(m·n) lower bound?
Model answer: Any unvisited cell could increase its row sum enough to change the max; must read all.
Q2. Code bug: max over accounts[i][0] only?
Model answer: Ignores other banks — wrong wealth. Always sum full row length n.
Q3. Space if you store all row sums first?
Model answer: O(m) extra; streaming max while summing each row is O(1).
✅ Solution Richest Customer Wealth
Problem Statement
You are given an m x n matrix accounts where accounts[i][j] is the amount of money the ith customer has in the jth bank.
Return the wealth that the richest customer has.
Imagine every customer has multiple bank accounts, with each account holding a certain amount of money. The total wealth of a customer is calculated by summing all the money across all their bank accounts.
Examples
-
Example 1:
- Input: accounts =
[[5,2,3], [0,6,7]] - Expected Output:
13 - Justification: The total wealth of the first customer is 10 and of the second customer is 13. So, the output is 13 as it's the maximum among all customers.
- Input: accounts =
-
Example 2:
- Input: accounts =
[[1,2], [3,4], [5,6]] - Expected Output:
11 - Justification: Total wealth for each customer is [3, 7, 11]. Maximum of these is 11.
- Input: accounts =
-
Example 3:
- Input: accounts =
[[5,10,15], [10,20,30], [15,30,45]]- Expected Output:
90 - Justification: Total wealth for each customer is [30, 60, 90]. The wealthiest customer has 90.
Constraints:
m == accounts.lengthn == accounts[i].length1 <= m, n <= 501 <= accounts[i][j] <= 100
Solution
The algorithm aims to traverse each customer's accounts, compute their total wealth by summing up the balances, and track the wealthiest customer found so far. We utilize a straightforward iterative method, whereby we loop through each customer and their respective accounts, calculating the sum of each customer's accounts, and then making a comparison against a stored maximum wealth variable. If the current customer's total wealth surpasses our stored maximum, we update our stored value. Upon completion of the iteration through all customers, our stored maximum wealth value represents the wealth of the richest customer. Simplicity and a single pass over every cell make this algorithm appealing: the nested customer/account loops visit each balance exactly once, which is optimal because every account can contribute to wealth and must be read.
Step-by-Step Algorithm
Step 1: Initialize a variable maxWealth to store and track the maximum wealth found during the iteration. Set its initial value to 0 as we will use it for comparison.
Step 2: Iterate through the 2D-array of accounts using a loop. Each sub-array represents one customer's accounts.
Step 3: For each customer, calculate their total wealth by summing up all the values in their respective sub-array. Utilize another loop or a sum function for this.
Step 4: Compare the computed sum of the current customer with maxWealth. If it’s greater, update maxWealth with the new value.
Step 5: Repeat steps 3-4 for all customers in the array.
Step 6: Once the iteration is complete, maxWealth holds the maximum wealth among all customers. Return this value as the output.
Algorithm Walkthrough
-
Initialize
maxWealthto 0: This will store the maximum wealth we find as we traverse through the accounts.maxWealth = 0 -
Iterating through the customer arrays:
-
First customer:
[5,10,15]- Calculate total wealth:
5 + 10 + 15 = 30 - Compare and update
maxWealth:maxWealth(0) <30=> UpdatemaxWealthto30
- Calculate total wealth:
-
Second customer:
[10,20,30]- Calculate total wealth:
10 + 20 + 30 = 60 - Compare and update
maxWealth:maxWealth(30) <60=> UpdatemaxWealthto60
- Calculate total wealth:
-
Third customer:
[15,30,45]- Calculate total wealth:
15 + 30 + 45 = 90 - Compare and update
maxWealth:maxWealth(60) <90=> UpdatemaxWealthto90
- Calculate total wealth:
-
-
Conclusion: The final value of
maxWealthis90, which represents the richest customer's wealth. So,90is returned as the output.
This approach guarantees we evaluate the total wealth of each customer and always maintain the wealthiest customer's wealth encountered thus far in our maxWealth variable, ensuring we find the correct answer by the end of our iterations.
Code
Here is the code for this algorithm:
class Solution {
public int maximumWealth(int[][] accounts) {
int maxWealth = 0; // Initialize maxWealth to 0
// Loop through each customer's accounts
for (int[] customer : accounts) {
int wealth = 0; // Variable to store the wealth of the current customer
// Loop through each account of the current customer and sum them up
for (int account : customer) {
wealth += account;
}
// Update maxWealth if the current customer's wealth is greater
if (wealth > maxWealth) {
maxWealth = wealth;
}
}
// Return the maximum wealth found
return maxWealth;
}
public static void main(String[] args) {
Solution sol = new Solution();
// Example test cases
System.out.println(
sol.maximumWealth(new int[][] { { 5, 2, 3 }, { 0, 6, 7 } })
); // 13
System.out.println(
sol.maximumWealth(new int[][] { { 1, 2 }, { 3, 4 }, { 5, 6 } })
); // 11
System.out.println(
sol.maximumWealth(
new int[][] { { 5, 10, 15 }, { 10, 20, 30 }, { 15, 30, 45 } }
)
); // 90
}
}
Complexity Analysis
Time Complexity
-
Outer loop (customers): The outer loop iterates over each customer in the
accountsarray. If there areMcustomers, this loop runstimes. -
Inner loop (accounts): For each customer, the inner loop iterates over the accounts to sum them. If each customer has
Naccounts, the inner loop runstimes for each customer. -
Therefore, the total time complexity is
, where Mis the number of customers, andNis the number of accounts per customer.
Overall time complexity:
Space Complexity
-
Constant space: The algorithm uses a few extra variables (
maxWealthandwealth), both of which require constant space,. -
No additional data structures that depend on the input size are used.
Overall space complexity:
🎯 STRICT STANDOUT: Why / worked+complexity / pattern+when-not / edge / drills — Solution Richest Customer Wealth (easy)
Why this concept exists (judgment layer)
Row-sum then max is the matrix baseline: visit every cell exactly once. Teaches that nested loops over m×n are Θ(m·n) and optimal when every entry can affect the answer — no 'avoid nested loops' magic.
Worked example with complexity derivation
accounts=[[5,10,15],[10,20,30],[15,30,45]]:
row sums 30, 60, 90 → maxWealth=90.
[[5,2,3],[0,6,7]] → 10 vs 13 → 13.
Outer m customers × inner n accounts: each of m·n cells read once → Θ(m·n) time, Θ(1) extra space.
Lower bound: any unread cell could increase some wealth → must read all → optimal.
Pattern + when-NOT / named alternative
PATTERN: aggregate per row / track global max (matrix reduction). WHEN NOT: need per-column max too → still one pass can track both; sparse matrices with mostly zeros might store COO but constraints here are dense small (m,n≤50). Prefix 2D would be overkill for single full-row sums.
Edge case / failure mode
Edges: m=1 or n=1; all equal wealth; min value 1 per constraints so maxWealth init 0 safe. Failure: claim O(m) by skipping accounts — wrong. Overflow irrelevant under tiny constraints.
Hostile-panel drills (defend the decision)
Q1. Why nested loops are required.
Model answer: Answer depends on every account balance; missing a cell can understate wealth.
Q2. Complexities.
Model answer: Time Θ(m·n), space Θ(1) extra.
Q3. Compute [[1,2],[3,4],[5,6]].
Model answer: Wealths 3,7,11 → 11.
Recognize it: Grid traversal / rotation / in-place marking → index arithmetic, or DFS/BFS over cells.
▶ Visualize this problem (step it, predict each fork)
🤖 Don't fully get this? Learn it with Claude
Stuck on Problem 1 Richest Customer Wealth? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Progressively stronger hints — you still solve it.
I'm working on the problem **Problem 1 Richest Customer Wealth** (DSA). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
See the technique, not just code.
Explain the optimal approach to **Problem 1 Richest Customer Wealth** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Catch bugs, edge cases, sub-optimality.
I'll paste my solution to **Problem 1 Richest Customer Wealth**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Lock in recognition with look-alikes.
Give me 2 problems that use the SAME underlying pattern as **Problem 1 Richest Customer Wealth**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.