파비의 매일매일 공부기록

Today's Challenge 본문

Problem Solving/LeetCode

Today's Challenge

fabichoi 2022. 5. 10. 23:45

https://leetcode.com/problems/combination-sum-iii/

 

Combination Sum III - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

itertools 패키지의 combinations을 사용하면 매우 간단히 풀 수 있다.
다른 사람이 푼 걸 보니 backtraking으로도 풀 수 있는 것 같다.

from itertools import combinations
class Solution:
    def combinationSum3(self, k: int, n: int) -> List[List[int]]:
        ar = [i for i in range(1,10)]
        res = []
        for c in combinations(ar, k):
            if sum(c) == n:
                res.append(c)
        return res
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

Today's Challenge  (0) 2022.05.12
Today's Challenge  (0) 2022.05.11
Today's Challenge  (0) 2022.05.09
Today's Challenge  (0) 2022.05.08
Today's Challenge  (0) 2022.05.07
Comments