[LeetCode][Python3] 77. Combinations
2019. 8. 25. 00:45 |
프로그래밍/LeetCode
Problem :
https://leetcode.com/problems/combinations/
My Solution :
class Solution:
def combine(self, n, k):
def comb(n, k):
if (n, k) in memo:
return memo[(n, k)]
if n < k:
return []
if k == 1:
memo[(n, k)] = [[x] for x in range(1, n+1)]
return memo[(n, k)]
part1 = comb(n-1, k-1)
part2 = comb(n-1, k)
res = []
for part in part1:
res.append(part + [n])
res.extend(part2)
memo[(n, k)] = res
return memo[(n, k)]
memo = dict()
return comb(n, k)
Comment :
nCr = n-1Cr-1 + n-1Cr 임을 활용하였으며, 중복 계산을 피하기 위해 memoization을 활용하였다.
'프로그래밍 > LeetCode' 카테고리의 다른 글
[LeetCode][Python3] 89. Gray Code (0) | 2019.09.03 |
---|---|
[LeetCode][Python3] 357. Count Numbers with Unique Digits (0) | 2019.08.29 |
[LeetCode][Python3] 996. Number of Squareful Arrays (0) | 2019.08.28 |
[LeetCode][Python3] 174. Dungeon Game (0) | 2019.08.28 |
[LeetCode][Python3] 39. Combination Sum (0) | 2019.08.23 |
[LeetCode][Python3] 216. Combination Sum III (0) | 2019.08.23 |
[LeetCode][Python3] 52. N-Queens II (0) | 2019.08.23 |
[LeetCode][Python3] 526. Beautiful Arrangement (0) | 2019.08.21 |
최근에 달린 댓글 최근에 달린 댓글