LeetCode 题解工作台
最富有客户的资产总量
给你一个 m x n 的整数网格 accounts ,其中 accounts[i][j] 是第 i 位客户在第 j 家银行托管的资产数量。返回最富有客户所拥有的 资产总量 。 客户的 资产总量 就是他们在各家银行托管的资产数量之和。最富有客户就是 资产总量 最大的客户。 …
2
题型
9
代码语言
3
相关题
当前训练重点
简单 · 数组·matrix
答案摘要
我们遍历 `accounts`,求出每一行的和的最大值即可。 时间复杂度 $O(m \times n)$,其中 和 分别为网格的行数和列数。空间复杂度 。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 数组·matrix 题型思路
题目描述
给你一个 m x n 的整数网格 accounts ,其中 accounts[i][j] 是第 i 位客户在第 j 家银行托管的资产数量。返回最富有客户所拥有的 资产总量 。
客户的 资产总量 就是他们在各家银行托管的资产数量之和。最富有客户就是 资产总量 最大的客户。
示例 1:
输入:accounts = [[1,2,3],[3,2,1]]
输出:6
解释:
第 1 位客户的资产总量 = 1 + 2 + 3 = 6
第 2 位客户的资产总量 = 3 + 2 + 1 = 6
两位客户都是最富有的,资产总量都是 6 ,所以返回 6 。
示例 2:
输入:accounts = [[1,5],[7,3],[3,5]] 输出:10 解释:第 1 位客户的资产总量= 6第 2 位客户的资产总量= 10第 3 位客户的资产总量= 8 第 2 位客户是最富有的,资产总量是 10
示例 3:
输入:accounts = [[2,8,7],[7,1,3],[1,9,5]] 输出:17
提示:
m == accounts.lengthn == accounts[i].length1 <= m, n <= 501 <= accounts[i][j] <= 100
解题思路
方法一:求和
我们遍历 accounts,求出每一行的和的最大值即可。
时间复杂度 ,其中 和 分别为网格的行数和列数。空间复杂度 。
class Solution:
def maximumWealth(self, accounts: List[List[int]]) -> int:
return max(sum(v) for v in accounts)
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Can the candidate optimize the solution for space while maintaining efficiency?
- question_mark
Does the candidate consider edge cases, such as multiple customers with the same wealth?
- question_mark
Is the candidate able to articulate the reasoning behind the iterative approach?
常见陷阱
外企场景- error
Forgetting to sum the accounts for each customer before comparing with the current maximum.
- error
Inefficient use of space, such as creating unnecessary data structures to hold intermediate sums.
- error
Not handling edge cases where all customers have the same wealth or where the input size is minimal.
进阶变体
外企场景- arrow_right_alt
Consider scenarios where the grid size grows larger, requiring more efficient solutions for both time and space.
- arrow_right_alt
Extend this problem by adding constraints where the values in the accounts array vary dynamically.
- arrow_right_alt
Modify the problem to calculate the wealth of the least wealthy customer instead of the richest.