일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
- shiba
- iterator
- LeetCode
- Regular Expression
- Convert Sorted List to Binary Search Tree
- 109. Convert Sorted List to Binary Search Tree
- DWG
- 43. Multiply Strings
- Python
- concurrency
- 715. Range Module
- t1
- kaggle
- data science
- 시바견
- Substring with Concatenation of All Words
- 프로그래머스
- Decorator
- 밴픽
- Protocol
- Class
- attribute
- Generator
- 컴퓨터의 구조
- 315. Count of Smaller Numbers After Self
- 파이썬
- 운영체제
- Python Implementation
- Python Code
- 30. Substring with Concatenation of All Words
- Today
- Total
Scribbling
LeetCode: 465. Optimal Account Balancing 본문
First, we look through all the transactions and check how much money(or debt) each one has. Array self.money is storing that information, and we excluded the ones who don't have any money or debt.
Next, let's define "helper(self, indexs)" as our goal function. It will return 'min # of transactions needed'. Here, we take indexes (tuple type) as the parameter of the function so that we can easily store returned results in a "self.dp" dictionary.
Now, the most important part: drawing out the recursion equation.
--> helper(indexes) = min(helper(rest) + k - 1), for all combinations of size k that add up to zero.
rest: indexes - indexes of a certain combination of size k that add up to zero.
<Example>
self.money: [3, -1, -2, 4, 1, 3]
helper((0, 1, 2, 3, 4, 5))
index 0, 1, 2 add up to zero (as 3 -1 -2 = 0), so k = 3 and rest is (3, 4, 5).
from itertools import combinations
class Solution:
def minTransfers(self, transactions: List[List[int]]) -> int:
money = [0] * 21
for f, t, m in transactions:
money[f] -= m
money[t] += m
self.money = [m for m in money if m != 0]
idxs = tuple([i for i in range(len(self.money))])
self.dp = {}
return self.helper(idxs)
def helper(self, idxs):
if not idxs:
return 0
if idxs in self.dp:
return self.dp[idxs]
ret = len(idxs) - 1
for k in range(2, len(idxs)//2+1):
for temp in combinations(idxs, k):
if sum([self.money[i] for i in temp]) == 0:
rest = tuple(set(idxs) - set(temp))
ret = min(ret, self.helper(rest) + k - 1)
self.dp[idxs] = ret
return ret
from itertools import combinations
import functools
class Solution:
def minTransfers(self, transactions: List[List[int]]) -> int:
money = [0] * 12
for f, t, m in transactions:
money[f] -= m
money[t] += m
self.money = [m for m in money if m != 0]
idxs = tuple([i for i in range(len(self.money))])
return self.helper(idxs)
@lru_cache()
def helper(self, idxs):
if not idxs:
return 0
ret = len(idxs) - 1
for k in range(2, len(idxs)//2+1):
for temp in combinations(idxs, k):
if sum([self.money[i] for i in temp]) == 0:
rest = tuple(set(idxs) - set(temp))
ret = min(ret, self.helper(rest) + k - 1)
return ret
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 990. Satisfiability of Equality Equations (0) | 2022.02.26 |
---|---|
LeetCode: 652. Find Duplicate Subtrees (0) | 2022.02.23 |
LeetCode: 410. Split Array Largest Sum (0) | 2022.02.17 |
LeetCode: 315. Count of Smaller Numbers After Self (0) | 2022.02.10 |
LeetCode: 135. Candy (0) | 2022.02.03 |