코딩 테스트 및 알고리즘/Grind 75 (Blind 75 Leetcode Questions)
Array: Combination Sum
띠리링구
2024. 2. 17. 23:10
https://leetcode.com/problems/combination-sum/description/
LeetCode - The World's Leading Online Programming Learning Platform
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
너무 쉬운 간단한 backtracking 문제
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
answer = []
def dfs(comb, ind, remaining):
if remaining == 0:
answer.append(comb[:])
return
for i in range(ind, len(candidates)):
if remaining >= candidates[i]:
dfs(comb + [candidates[i]], i, remaining - candidates[i])
dfs([], 0, target)
return answer