Skip to content

Commit

Permalink
problem: 0022 0040 0216 combinations
Browse files Browse the repository at this point in the history
  • Loading branch information
cychiu8 committed Apr 27, 2022
1 parent 885dd80 commit 892b1a0
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 0 deletions.
55 changes: 55 additions & 0 deletions 0022 Generate Parentheses/0022 Generate Parentheses.md
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

![Untitled](./Untitled.png)

- 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)**
Binary file added 0022 Generate Parentheses/Untitled.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 51 additions & 0 deletions 0040 Combination Sum II/0040 Combination Sum II.md
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
```
51 changes: 51 additions & 0 deletions 0216 Combination Sum III/0216 Combination Sum III.md
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
```

0 comments on commit 892b1a0

Please sign in to comment.