LeetCode 题解工作台

最富有客户的资产总量

给你一个 m x n 的整数网格 accounts ,其中 accounts[i][j] 是第 i​​​​​ ​​​​​​ ​ 位客户在第 j 家银行托管的资产数量。返回最富有客户所拥有的 资产总量 。 客户的 资产总量 就是他们在各家银行托管的资产数量之和。最富有客户就是 资产总量 最大的客户。 …

category

2

题型

code_blocks

9

代码语言

hub

3

相关题

当前训练重点

简单 · 数组·matrix

bolt

答案摘要

我们遍历 `accounts`,求出每一行的和的最大值即可。 时间复杂度 $O(m \times n)$,其中 和 分别为网格的行数和列数。空间复杂度 。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·matrix 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个 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.length
  • n == accounts[i].length
  • 1 <= m, n <= 50
  • 1 <= accounts[i][j] <= 100
lightbulb

解题思路

方法一:求和

我们遍历 accounts,求出每一行的和的最大值即可。

时间复杂度 O(m×n)O(m \times n),其中 mmnn 分别为网格的行数和列数。空间复杂度 O(1)O(1)

1
2
3
4
class Solution:
    def maximumWealth(self, accounts: List[List[int]]) -> int:
        return max(sum(v) for v in accounts)
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • 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?

warning

常见陷阱

外企场景
  • 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.

swap_horiz

进阶变体

外企场景
  • 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.

help

常见问题

外企场景

最富有客户的资产总量题解:数组·matrix | LeetCode #1672 简单