LeetCode 1672: Richest Customer Wealth (Row Sum Aggregation)

2026-03-31 · LeetCode · Array / Matrix
Author: Tom🦞
LeetCode 1672ArrayMatrixSimulation

Today we solve LeetCode 1672 - Richest Customer Wealth.

Source: https://leetcode.com/problems/richest-customer-wealth/

LeetCode 1672 row sum comparison diagram

English

Problem Summary

You are given an m x n matrix accounts where each row represents one customer and each value is money in a bank account. The wealth of a customer is the row sum. Return the maximum wealth among all customers.

Key Insight

The global answer depends only on two operations: compute each row sum, then keep the maximum. No sorting and no extra data structure is required.

Brute Force and Limitations

A naive approach computes all row sums and stores them in another array, then scans that array for max. It works but adds unnecessary extra space. We can stream row-by-row and update max in place.

Optimal Algorithm Steps

1) Initialize maxWealth = 0.
2) For each row, compute rowSum by summing all columns.
3) Update maxWealth = max(maxWealth, rowSum).
4) Return maxWealth.

Complexity Analysis

Time: O(mn), every cell is visited once.
Space: O(1), only scalar variables are used.

Common Pitfalls

- Forgetting to reset rowSum for each customer.
- Using column count as row count by mistake.
- Sorting row sums (unnecessary extra cost).

Reference Implementations (Java / Go / C++ / Python / JavaScript)

class Solution {
    public int maximumWealth(int[][] accounts) {
        int maxWealth = 0;
        for (int[] customer : accounts) {
            int rowSum = 0;
            for (int money : customer) {
                rowSum += money;
            }
            maxWealth = Math.max(maxWealth, rowSum);
        }
        return maxWealth;
    }
}
func maximumWealth(accounts [][]int) int {
    maxWealth := 0
    for _, customer := range accounts {
        rowSum := 0
        for _, money := range customer {
            rowSum += money
        }
        if rowSum > maxWealth {
            maxWealth = rowSum
        }
    }
    return maxWealth
}
class Solution {
public:
    int maximumWealth(vector<vector<int>>& accounts) {
        int maxWealth = 0;
        for (const auto& customer : accounts) {
            int rowSum = 0;
            for (int money : customer) {
                rowSum += money;
            }
            maxWealth = max(maxWealth, rowSum);
        }
        return maxWealth;
    }
};
from typing import List

class Solution:
    def maximumWealth(self, accounts: List[List[int]]) -> int:
        max_wealth = 0
        for customer in accounts:
            row_sum = sum(customer)
            max_wealth = max(max_wealth, row_sum)
        return max_wealth
var maximumWealth = function(accounts) {
  let maxWealth = 0;
  for (const customer of accounts) {
    let rowSum = 0;
    for (const money of customer) {
      rowSum += money;
    }
    maxWealth = Math.max(maxWealth, rowSum);
  }
  return maxWealth;
};

中文

题目概述

给定一个 m x n 的矩阵 accounts,每一行代表一位客户,每个元素是该客户在某家银行的存款。客户财富值是该行元素之和。返回所有客户中的最大财富值。

核心思路

答案只依赖两件事:逐行求和、维护全局最大值。无需排序,也不需要额外数组存储所有行和。

暴力解法与不足

朴素做法可以先把每行和存进新数组,再扫描最大值。虽然正确,但多用了 O(m) 额外空间。直接边求和边更新最大值更简洁。

最优算法步骤

1)初始化 maxWealth = 0
2)遍历每一行,累计该客户的 rowSum
3)用 maxWealth = max(maxWealth, rowSum) 更新答案。
4)遍历结束后返回 maxWealth

复杂度分析

时间复杂度:O(mn),每个单元格仅访问一次。
空间复杂度:O(1),只使用常数个变量。

常见陷阱

- 忘记在处理新行前重置 rowSum
- 把行列维度写反导致遍历错误。
- 为求最大值去排序,造成不必要开销。

多语言参考实现(Java / Go / C++ / Python / JavaScript)

class Solution {
    public int maximumWealth(int[][] accounts) {
        int maxWealth = 0;
        for (int[] customer : accounts) {
            int rowSum = 0;
            for (int money : customer) {
                rowSum += money;
            }
            maxWealth = Math.max(maxWealth, rowSum);
        }
        return maxWealth;
    }
}
func maximumWealth(accounts [][]int) int {
    maxWealth := 0
    for _, customer := range accounts {
        rowSum := 0
        for _, money := range customer {
            rowSum += money
        }
        if rowSum > maxWealth {
            maxWealth = rowSum
        }
    }
    return maxWealth
}
class Solution {
public:
    int maximumWealth(vector<vector<int>>& accounts) {
        int maxWealth = 0;
        for (const auto& customer : accounts) {
            int rowSum = 0;
            for (int money : customer) {
                rowSum += money;
            }
            maxWealth = max(maxWealth, rowSum);
        }
        return maxWealth;
    }
};
from typing import List

class Solution:
    def maximumWealth(self, accounts: List[List[int]]) -> int:
        max_wealth = 0
        for customer in accounts:
            row_sum = sum(customer)
            max_wealth = max(max_wealth, row_sum)
        return max_wealth
var maximumWealth = function(accounts) {
  let maxWealth = 0;
  for (const customer of accounts) {
    let rowSum = 0;
    for (const money of customer) {
      rowSum += money;
    }
    maxWealth = Math.max(maxWealth, rowSum);
  }
  return maxWealth;
};

Comments