Here is the leetcode question: [Richest customer wealth][1]
and here is my solution:
class Solution:
def maximumWealth(self, accounts: list[list[int]]) -> int:
res = []
for wealth in accounts:
res = (sum(wealth))
return max(res)
and I got the error such as:
TypeError: 'int' object is not iterable
return max(res)
Line 6 in maximumWealth (Solution.py)
ret = Solution().maximumWealth(param_1)
Line 25 in _driver (Solution.py)
_driver()
Line 36 in <module> (Solution.py)
I simply converted this code into code above but I don’t know why this works and the another doesn’t?
class Solution:
def maximumWealth(self, accounts: list[list[int]]) -> int:
# 리스트 내포 사용
accounts = [sum(wealth) for wealth in accounts]
return max(accounts)
>Solution :
You want to add your wealth summary to a list and not replace it. So instead of
res = []
for wealth in accounts:
res = (sum(wealth))
you want last line to look like
res.append(sum(wealth))