//GitHub https://github.com/vellimole0621
vellimole0621 - Overview
キム・ヒョンギュ . vellimole0621 has 6 repositories available. Follow their code on GitHub.
github.com
1672. Richest Customer Wealth
https://leetcode.com/problems/richest-customer-wealth/
Richest Customer Wealth - LeetCode
Can you solve this real interview question? Richest Customer Wealth - You are given an m x n integer grid accounts where accounts[i][j] is the amount of money the i th customer has in the j th bank. Return the wealth that the richest customer has. A custom
leetcode.com
문제 설명
-
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.
=> 2차원 배열, 각 행의 해당 열의 합을 비교해, 가장 큰 행의 열의 합을 찾아라.
내 풀이
-
이중 반복문으로 temp로 각 행별 열의 합을 찾아 비교
# 1672. Richest Customer Wealth
# https://leetcode.com/problems/richest-customer-wealth/
class Solution:
def maximumWealth(self, accounts: List[List[int]]) -> int:
members = len(accounts)
num_money = len(accounts[0])
best_money = 0
for i in range(0, members):
temp = 0
for j in range(0, num_money):
temp += accounts[i][j]
if temp > best_money:
best_money = temp
return best_money
피드백
-
풀이 결과, 메모리 사용이 많았다. 좀 더 효율적인 풀이를 찾은 결과. 아래와 같은 풀이를 찾을 수 있었는데.
class Solution:
def maximumWealth(self, accounts: List[List[int]]) -> int:
richest = 0
for customer in accounts:
wealth = sum(customer)
if wealth >= richest:
richest=+wealth
return richest
풀이 방식은 거의 동일하였으나, 내장 함수 sum을 이용해 더 효율적인 풀이를 하였다. 연산의 경우 내장 함수를 사용하는 편이 좋다는 결론을 도출하였다.
'프로그래밍 > Python' 카테고리의 다른 글
[파이썬 Python/알고리즘] LeetCode - Number of Steps to Reduce a Number to Zero (0) | 2023.07.31 |
---|---|
[파이썬 Python/알고리즘] LeetCode - Fizz Buzz (0) | 2023.07.31 |
[파이썬 Python/알고리즘] LeetCode - Integer to Roman (0) | 2023.07.29 |
[파이썬 Python/알고리즘] LeetCode - Valid Perfect Square (0) | 2023.07.19 |
[파이썬 Python/알고리즘] LeetCode - Unique Paths (0) | 2023.07.16 |