Skip to content

Commit 8e5db80

Browse files
committed
2 parents 721bbea + 885dd80 commit 8e5db80

File tree

5 files changed

+142
-0
lines changed

5 files changed

+142
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# 39. Combination Sum
2+
3+
* Difficulty: medium
4+
* Link: https://leetcode.com/problems/combination-sum/
5+
* Topics: Backtracking
6+
7+
# Clarification
8+
9+
1. Check the inputs and outputs
10+
- INPUT:
11+
- List[int] candiates
12+
- int target
13+
- OUTPUT: List[List[int]]
14+
15+
# Naive Solution
16+
17+
### Thought Process
18+
19+
1. 停止條件:
20+
1. 加總等於 target
21+
2. 加總大於 target
22+
2. subset = 現有 + candiate[i]
23+
3. 新的 candidate 則是從 candidate[i] 之後開始 (包含 candidate[i], 因為 The **same**
24+
 number may be chosen from `candidates` an **unlimited number of times**.)
25+
26+
![Untitled](./Untitled.png)
27+
28+
- Implement
29+
30+
```python
31+
class Solution(object):
32+
def combinationSum(self, candidates, target):
33+
"""
34+
:type candidates: List[int]
35+
:type target: int
36+
:rtype: List[List[int]]
37+
"""
38+
result = []
39+
def backtrack(subset, res):
40+
if sum(subset) == target:
41+
return result.append(subset)
42+
elif sum(subset) > target:
43+
return
44+
for i in range(len(res)):
45+
backtrack(subset + [res[i]], res[i:])
46+
backtrack([], candidates)
47+
return result
48+
```
49+
50+
51+
### Complexity
52+
53+
- Time complexity:
54+
- $O(n^n)$
55+
56+
![737308D1-A159-4D3A-A961-D71B98C17CB6.jpeg](./737308D1-A159-4D3A-A961-D71B98C17CB6.jpeg)
57+
58+
- $O(k*2^n)$
59+
- **[Deadbeef-ECE 039_Combination_Sum.java](https://github.com/Deadbeef-ECE/Interview/blob/master/Leetcode/BackTracking/039_Combination_Sum.java)**
60+
- Space complexity:
61+
62+
### Problems & Improvement
63+
64+
- 可以更早排除不須 backtrack 的數字
65+
- 當 candidate > target 時就不用進去 backtrack 了
66+
- Implement
67+
68+
```python
69+
class Solution(object):
70+
def combinationSum(self, candidates, target):
71+
"""
72+
:type candidates: List[int]
73+
:type target: int
74+
:rtype: List[List[int]]
75+
"""
76+
result = []
77+
def backtrack(subset, res):
78+
if sum(subset) == target:
79+
return result.append(subset)
80+
elif sum(subset) > target:
81+
return
82+
for i in range(len(res)):
83+
if res[i] > target:
84+
return
85+
backtrack(subset + [res[i]], res[i:])
86+
backtrack([], candidates)
87+
return result
88+
```
Loading

0039 Combination Sum/Untitled.png

210 KB
Loading

0077 Combinations/77 Combinations.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# 77. Combinations
2+
3+
* Difficulty: medium
4+
* Link: https://leetcode.com/problems/combinations/
5+
* Topics: Backtracking
6+
7+
# Clarification
8+
9+
1. Check the inputs and outputs
10+
- INPUT:
11+
- int n : the range [1,n]
12+
- int k : pick k numbers
13+
- OUTPUT: List[List[int]]
14+
15+
# Naive Solution
16+
17+
### Thought Process
18+
19+
- [NOT working] 用於 k 為少數時
20+
1. 使用兩個 pointer
21+
2. 第一個放一開始,第二個從第一個的下一個開始移動
22+
3. 直到第二個 pointer 指到 Null
23+
- 停止條件:當 subset 的長度 = k 時
24+
25+
![Untitled](./Untitled.png)
26+
27+
- Implement
28+
29+
```python
30+
class Solution(object):
31+
def combine(self, n, k):
32+
"""
33+
:type n: int
34+
:type k: int
35+
:rtype: List[List[int]]
36+
"""
37+
result = []
38+
def combination(subset, res):
39+
if len(subset) == k:
40+
return result.append(subset)
41+
for i in range(len(res)):
42+
combination(subset + [res[i]], res[i+1:])
43+
combination([], range(1,n+1))
44+
return result
45+
```
46+
47+
48+
### Complexity
49+
50+
- Time complexity:
51+
- O(C(n,k))
52+
- $C^n_k=(n!)/k!(n-k)!$
53+
- Space complexity: O(k)
54+
- subset 的大小

0077 Combinations/Untitled.png

177 KB
Loading

0 commit comments

Comments
 (0)