Solution
from typing import List
class Solution:
# [1, n] -> (1, n + 1) ๋ฒ์
def combine(self, n: int, k: int) -> List[List[int]]:
answer = []
def dfs(start: int, index: int, elements: List[int]) -> None:
if len(elements) == k:
answer.append(elements[:])
return
for i in range(start, n + 1):
elements.append(i)
dfs(i + 1, index + 1, elements)
elements.pop()
dfs(1, k, [])
return answer
import itertools
from typing import List
class Solution:
def combine(self, n: int, k: int) -> List[List[int]]:
return itertools.combinations(range(1, n + 1), k)
Leave a comment