-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
problem: 0022 0040 0216 combinations
- Loading branch information
Showing
4 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# 0022. Generate Parentheses | ||
|
||
* Difficulty: medium | ||
* Link: https://leetcode.com/problems/generate-parentheses/ | ||
* Topics: Backtracking | ||
|
||
# Clarification | ||
|
||
1. Check the inputs and outputs | ||
- INPUT: | ||
- n: int | ||
- OUTPUT: List[String] | ||
|
||
# Naive Solution | ||
|
||
### Thought Process | ||
|
||
- Backtracking | ||
- 停止條件 | ||
- open == n and close == n | ||
- 非停止條件 | ||
- 當 open < n | ||
- subset = subset + open | ||
- 當 close < open | ||
- subset = subest + close | ||
|
||
 | ||
|
||
- Implement | ||
|
||
```python | ||
class Solution(object): | ||
def generateParenthesis(self, n): | ||
""" | ||
:type n: int | ||
:rtype: List[str] | ||
""" | ||
result = [] | ||
|
||
def backtracking(substr, num_open, num_close): | ||
if num_open == n and num_close == n: | ||
return result.append(substr) | ||
if num_open < n: | ||
backtracking(substr + "(", num_open + 1, num_close) | ||
if num_close < num_open: | ||
backtracking(substr + ")", num_open, num_close+1) | ||
|
||
backtracking("", 0,0) | ||
return result | ||
``` | ||
|
||
|
||
# Note | ||
|
||
- **[Generate Parentheses - Stack - Leetcode 22](https://www.youtube.com/watch?v=s9fokUqJ76A)** |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# 40. Combination Sum II | ||
|
||
* Difficulty: medium | ||
* Link: https://leetcode.com/problems/combination-sum-ii/ | ||
* Topics: Array-String, Backtracking | ||
|
||
# Clarification | ||
|
||
1. Check the inputs and outputs | ||
- INPUT: | ||
- canditates: List[int] | ||
- target: int | ||
- OUTPUT: List[List[int] | ||
|
||
# Naive Solution | ||
|
||
### Thought Process | ||
|
||
- Backtracking | ||
- 停止條件 | ||
- sum of subset == target : add to result | ||
- sum of subset > target : return | ||
- 未達停止條件 | ||
- subset + res[i] | ||
- res = res[i+1:] | ||
- 注意:當 res[i] == res[i+1] 時跳過,避免重複 | ||
- Implement | ||
|
||
```python | ||
class Solution(object): | ||
def combinationSum2(self, candidates, target): | ||
""" | ||
:type candidates: List[int] | ||
:type target: int | ||
:rtype: List[List[int]] | ||
""" | ||
candidates.sort() | ||
result = [] | ||
def backtracking(subset, res): | ||
if sum(subset) == target: | ||
return result.append(subset) | ||
if sum(subset) > target: | ||
return | ||
for idx in range(len(res)): | ||
if idx > 0 and res[idx] == res[idx-1]: | ||
continue | ||
backtracking(subset + [res[idx]], res[idx+1:]) | ||
|
||
backtracking([], candidates) | ||
return result | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# 216. Combination Sum III | ||
|
||
* Difficulty: medium | ||
* Link: https://leetcode.com/problems/combination-sum-iii/ | ||
* Topics: Backtracking | ||
|
||
# Clarification | ||
|
||
1. Check the inputs and outputs | ||
- INPUT: | ||
- k: int | ||
- n: int (target) | ||
- OUTPUT:List[List[int]] | ||
|
||
# Naive Solution | ||
|
||
### Thought Process | ||
|
||
- Backtracking | ||
- 停止條件 | ||
- len(subset) == k and sum(subset) == n ⇒ add to result | ||
- sum(subset) > n ⇒ return | ||
- len(subset) > k ⇒ return | ||
- 非停止條件 | ||
- subset = subset + res[i] | ||
- res = res[i+1:] | ||
- Implement | ||
|
||
```python | ||
class Solution(object): | ||
def combinationSum3(self, k, n): | ||
""" | ||
:type k: int | ||
:type n: int | ||
:rtype: List[List[int]] | ||
""" | ||
result = [] | ||
candidates = range(1,10) | ||
def backtracking(subset, res): | ||
if len(subset) == k and sum(subset) == n: | ||
return result.append(subset) | ||
if len(subset) > k: | ||
return | ||
if sum(subset) > n: | ||
return | ||
for idx, num in enumerate(res): | ||
backtracking(subset + [res[idx]], res[idx+1:]) | ||
|
||
backtracking([], candidates) | ||
return result | ||
``` |