Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given an m x n integer grid 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.
A customer's wealth is the amount of money they have in all their bank accounts. The richest customer is the customer that has the maximum wealth.
Example 1:
Input: accounts = [[1,2,3],[3,2,1]] Output: 6 Explanation:1st customer has wealth = 1 + 2 + 3 = 62nd customer has wealth = 3 + 2 + 1 = 6Both customers are considered the richest with a wealth of 6 each, so return 6.
Example 2:
Input: accounts = [[1,5],[7,3],[3,5]] Output: 10 Explanation: 1st customer has wealth = 6 2nd customer has wealth = 10 3rd customer has wealth = 8 The 2nd customer is the richest with a wealth of 10.
Example 3:
Input: accounts = [[2,8,7],[7,1,3],[1,9,5]] Output: 17
Constraints:
m == accounts.lengthn == accounts[i].length1 <= m, n <= 501 <= accounts[i][j] <= 100Problem summary: You are given an m x n integer grid 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. A customer's wealth is the amount of money they have in all their bank accounts. The richest customer is the customer that has the maximum wealth.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[[1,2,3],[3,2,1]]
[[1,5],[7,3],[3,5]]
[[2,8,7],[7,1,3],[1,9,5]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1672: Richest Customer Wealth
class Solution {
public int maximumWealth(int[][] accounts) {
int ans = 0;
for (var e : accounts) {
// int s = Arrays.stream(e).sum();
int s = 0;
for (int v : e) {
s += v;
}
ans = Math.max(ans, s);
}
return ans;
}
}
// Accepted solution for LeetCode #1672: Richest Customer Wealth
func maximumWealth(accounts [][]int) int {
ans := 0
for _, e := range accounts {
s := 0
for _, v := range e {
s += v
}
if ans < s {
ans = s
}
}
return ans
}
# Accepted solution for LeetCode #1672: Richest Customer Wealth
class Solution:
def maximumWealth(self, accounts: List[List[int]]) -> int:
return max(sum(v) for v in accounts)
// Accepted solution for LeetCode #1672: Richest Customer Wealth
impl Solution {
pub fn maximum_wealth(accounts: Vec<Vec<i32>>) -> i32 {
accounts.iter().map(|v| v.iter().sum()).max().unwrap()
}
}
// Accepted solution for LeetCode #1672: Richest Customer Wealth
function maximumWealth(accounts: number[][]): number {
return accounts.reduce(
(r, v) =>
Math.max(
r,
v.reduce((r, v) => r + v),
),
0,
);
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.