|
| 1 | +from typing import List |
| 2 | +from unittest import TestCase, main |
| 3 | +from collections import defaultdict |
| 4 | + |
| 5 | + |
| 6 | +class Solution: |
| 7 | + def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: |
| 8 | + return self.solveWithBackTracking(candidates, target) |
| 9 | + |
| 10 | + """ |
| 11 | + Runtime: 2039 ms (Beats 5.01%) |
| 12 | + Time Complexity: O(c * c * log c) |
| 13 | + - 처음 stack의 크기는 c에 비례 O(c) |
| 14 | + - 중복 제거에 사용하는 변수인 curr_visited_checker 생성에 O(c' log c') |
| 15 | + - stack의 내부 로직에서 c에 비례한 for문을 순회하는데 O(c) |
| 16 | + > O(c) * O(c' log c') + O(c) * O(c) ~= O(c * c * log c) |
| 17 | + |
| 18 | + Memory: 16.81 MB (Beats 11.09%) |
| 19 | + Space Complexity: O(c * c) |
| 20 | + - curr_combination의 크기가 c에 비례 |
| 21 | + - stack의 크기는 curr_combination의 크기와 c에 비례 |
| 22 | + > O(c * c) |
| 23 | + """ |
| 24 | + def solveWithDFS(self, candidates: List[int], target: int) -> List[List[int]]: |
| 25 | + result = [] |
| 26 | + stack = [] |
| 27 | + visited = defaultdict(bool) |
| 28 | + for candidate in candidates: |
| 29 | + stack.append([[candidate], candidate]) |
| 30 | + |
| 31 | + while stack: |
| 32 | + curr_combination, curr_sum = stack.pop() |
| 33 | + curr_visited_checker = tuple(sorted(curr_combination)) |
| 34 | + |
| 35 | + if curr_sum == target and visited[curr_visited_checker] is False: |
| 36 | + visited[curr_visited_checker] = True |
| 37 | + result.append(curr_combination) |
| 38 | + |
| 39 | + if target < curr_sum: |
| 40 | + continue |
| 41 | + |
| 42 | + for candidate in candidates: |
| 43 | + post_combination, post_sum = curr_combination + [candidate], curr_sum + candidate |
| 44 | + stack.append([post_combination, post_sum]) |
| 45 | + |
| 46 | + return result |
| 47 | + |
| 48 | + """ |
| 49 | + Runtime: 58 ms (Beats 32.30%) |
| 50 | + Time Complexity: O(c * c) |
| 51 | + - candidates 정렬에 O(log c) |
| 52 | + - 첫 depte에서 dfs 함수 호출에 O(c) |
| 53 | + - 그 후 candidates의 길이에 비례해서 재귀적으로 dfs를 호출하는데 O(c) |
| 54 | + - lower_bound_idx에 따라 range가 감소하기는 하나 일단은 비례 O(c') |
| 55 | + > O(log c) + O(c * c') ~= O(c * c), 단 c' <= c 이므로 이 복잡도는 upper bound |
| 56 | + Memory: 16.59 MB (Beats 75.00%) |
| 57 | + Space Complexity: O(c) |
| 58 | + - result를 제외하고 모두 nonlocal 변수를 call by reference로 참조 |
| 59 | + - dfs 함수 호출마다 메모리가 증가하는데, 호출횟수는 candidates의 길이에 비례 O(c) |
| 60 | + - lower_bound_idx에 따라 range가 감소하기는 하나 일단은 비례 |
| 61 | + > O(c), 단 이 복잡도는 upper bound |
| 62 | + """ |
| 63 | + def solveWithBackTracking(self, candidates: List[int], target: int) -> List[List[int]]: |
| 64 | + def dfs(stack: List[int], sum: int, lower_bound_idx: int): |
| 65 | + nonlocal result, candidates, target |
| 66 | + |
| 67 | + if target < sum: |
| 68 | + return |
| 69 | + elif sum < target: |
| 70 | + for idx in range(lower_bound_idx, len(candidates)): |
| 71 | + dfs(stack + [candidates[idx]], sum + candidates[idx], idx) |
| 72 | + else: # target == sum |
| 73 | + result.append(stack) |
| 74 | + return |
| 75 | + |
| 76 | + result = [] |
| 77 | + candidates.sort() |
| 78 | + dfs([], 0, 0) |
| 79 | + return result |
| 80 | + |
| 81 | + |
| 82 | +class _LeetCodeTestCases(TestCase): |
| 83 | + def test_1(self): |
| 84 | + candidates = [2, 3, 6, 7] |
| 85 | + target = 7 |
| 86 | + output = [[2, 2, 3], [7]] |
| 87 | + self.assertEqual(Solution.combinationSum(Solution(), candidates, target), output) |
| 88 | + |
| 89 | + def test_2(self): |
| 90 | + candidates = [2, 3, 5] |
| 91 | + target = 8 |
| 92 | + output = [[2, 2, 2, 2], [2, 3, 3], [3, 5]] |
| 93 | + self.assertEqual(Solution.combinationSum(Solution(), candidates, target), output) |
| 94 | + |
| 95 | + def test_3(self): |
| 96 | + candidates = [2] |
| 97 | + target = 1 |
| 98 | + output = [] |
| 99 | + self.assertEqual(Solution.combinationSum(Solution(), candidates, target), output) |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == '__main__': |
| 103 | + main() |
0 commit comments