박상길, 『파이썬 알고리즘 인터뷰』를 정리한 내용입니다.
https://leetcode.com/problems/combination-sum/
Combination Sum - LeetCode
Can you solve this real interview question? Combination Sum - Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the comb
leetcode.com
class Solution:
def combinationSum(self, candidates: [int], target: int) -> [[int]]:
candidates.sort()
answer = []
def dfs(nums: [int], remaining: int, index):
if remaining == 0:
answer.append(nums)
return
for i in range(index, len(candidates)):
if remaining < candidates[i]:
return
dfs(nums+[candidates[i]], remaining - candidates[i], i)
dfs([], target, 0)
return answer
[프로그래머스] 258711. 도넛과 막대 그래프| 그래프, BFS | 파이썬, 소스코드, 정답 (1) | 2024.04.03 |
---|---|
[프로그래머스] 258712. 가장 많이 받은 선물| 구현 | 파이썬, 소스코드, 정답 (1) | 2024.03.26 |
[LeetCode] Longest Substring Without Repeating Characters | 투포인터 | 파이썬 알고리즘 인터뷰, 정답, 소스코드 (1) | 2023.06.19 |
[LeetCode] Remove Duplicate Letters | stack | 파이썬 알고리즘 인터뷰, 정답, 소스코드 (0) | 2023.06.15 |
[프로그래머스] 148653. 마법의 엘리베이터| deque | 파이썬, 소스코드, 정답 (0) | 2023.06.14 |