일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- iterator
- kaggle
- 컴퓨터의 구조
- LeetCode
- 시바견
- 315. Count of Smaller Numbers After Self
- Decorator
- 밴픽
- data science
- Generator
- DWG
- Python Code
- concurrency
- 파이썬
- Protocol
- attribute
- 43. Multiply Strings
- Python Implementation
- shiba
- Regular Expression
- t1
- Python
- 프로그래머스
- 109. Convert Sorted List to Binary Search Tree
- Class
- 30. Substring with Concatenation of All Words
- Convert Sorted List to Binary Search Tree
- 운영체제
- Substring with Concatenation of All Words
- 715. Range Module
Archives
- Today
- Total
Scribbling
39. Combination Sum 본문
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
ret = []
arr = [0]
self.helper(ret, arr, candidates, target)
return ret
def helper(self, ret, arr, candidates, target):
_sum = sum(arr)
if _sum > target:
return
if _sum == target:
ret.append(arr[1:])
return
for candidate in candidates:
last_input = arr[-1]
if candidate >= last_input:
next_arr = arr.copy()
next_arr.append(candidate)
self.helper(ret, next_arr, candidates, target)
좀 더 좋은 solution...
본질적으로는 dfs 라는 점을 파악하면 더 쉽게 접근 가능,,,
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
ret = []
self.dfs(candidates, target, [], ret)
return ret
def dfs(self, nums, target, path, ret):
if target < 0:
return
if target == 0:
ret.append(path)
return
for i in range(len(nums)):
self.dfs(nums[i:], target-nums[i], path+[nums[i]], ret)
'Computer Science > Coding Test' 카테고리의 다른 글
44. Wildcard Matching (0) | 2021.09.06 |
---|---|
LeetCode: 48. Rotate Image (0) | 2021.09.05 |
32. Longest Valid Parentheses (0) | 2021.09.04 |
LeetCode: 29. Divide Two Integers (0) | 2021.09.02 |
LeetCode: 50. Pow(x, n) (0) | 2021.09.02 |