Skip to content

Commit

Permalink
78 subsets bfs
Browse files Browse the repository at this point in the history
  • Loading branch information
DayuanTan committed Jun 3, 2022
1 parent ff360f8 commit 5241d58
Show file tree
Hide file tree
Showing 8 changed files with 346 additions and 10 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ for x in nodes:
## 5.5 BFS for “all plans” problems 使用宽度优先搜索找所有方案
- One plan == one path
- All plans == find all paths (like connected-component problems)
- Medium [78. Subsets]() https://leetcode-cn.com/problems/subsets/
- Medium [78. Subsets](leetcode/78.subset.md) DFS/BFS
- Hard [297. Serialize and Deserialize Binary Tree] https://leetcode-cn.com/problems/serialize-and-deserialize-binary-tree/

## 5.6 Bidirectional BFS 双向
Expand Down Expand Up @@ -872,15 +872,16 @@ More BFD on Matrix
## 8Practice

- Combinations
- Medium [39. Combination Sum](leetcode/39.combination_sum.md) distinct
- Medium [40. Combination Sum II](leetcode/40.combination_sum2.md) duplicate
- Medium [216. Combination Sum III](leetcode/216.combination_sum3.md) k members sum up to target (Medium ==i90. K Sum II)
- Medium [39. Combination Sum](leetcode/39.combination_sum.md) distinct, use multiple times
- Medium [40. Combination Sum II](leetcode/40.combination_sum_ii.md) duplicate, use once
- Medium [216. Combination Sum III](leetcode/216.combination_sum3.md) k members sum up to target (Medium ==i90. K Sum II), use once
- Medium [377. Combination Sum IV](leetcode/377.combination_sum4.md) total amount of combinations (== Hard i89. K Sum) - DP 以后再做
- Medium [77. Combinations](leetcode/77.combinations.md) Recursion, Stack以后再做
- Medium [17. Letter Combinations of a Phone Number](leetcode/17.Letter_Combinations_of_a_Phone_Number.md)
- II

- 79. Word Search

DFS on Matrix
- Medium [79. Word Search](leetcode/79.word_search.md)
- 212. Word Search II
- i1848 · Word Search III

Expand Down
4 changes: 3 additions & 1 deletion leetcode/17.Letter_Combinations_of_a_Phone_Number.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

中等

https://leetcode.cn/problems/letter-combinations-of-a-phone-number/

Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order.

A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
Expand Down Expand Up @@ -97,5 +99,5 @@ class Solution:
for currletter in KEYBOARDS[digits[currIndex]]:
currSubset.append(currletter)
self.dfs(digits, digitsLeng, currIndex + 1, currSubset, results)
currSubset.pop() #cannot remove bc it may delete repeated letter
currSubset.pop() #cannot use remove() bc it may delete repeated letter
```
84 changes: 84 additions & 0 deletions leetcode/216.combination_sum3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
216. Combination Sum III

中等


https://leetcode.cn/problems/combination-sum-iii/


Find all valid combinations of k numbers that sum up to n such that the following conditions are true:

Only numbers 1 through 9 are used.
Each number is used at most once.
Return a list of all possible valid combinations. The list must not contain the same combination twice, and the combinations may be returned in any order.


```
Example 1:
Input: k = 3, n = 7
Output: [[1,2,4]]
Explanation:
1 + 2 + 4 = 7
There are no other valid combinations.
Example 2:
Input: k = 3, n = 9
Output: [[1,2,6],[1,3,5],[2,3,4]]
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input: k = 4, n = 1
Output: []
Explanation: There are no valid combinations.
Using 4 different numbers in the range [1,9], the smallest sum we can get is 1+2+3+4 = 10 and since 10 > 1, there are no valid combination.
```

Constraints:
```
2 <= k <= 9
1 <= n <= 60
```

相关企业

- 亚马逊 Amazon|3
- 谷歌 Google|3
- Facebook|2

相关标签
- Array
- Backtracking

相似题目
- Combination Sum
中等


```py
class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
if n < 0:
return []

nums = [i for i in range(1, 10)]
results = []
self.dfs(nums, 0, [], k, n, results)
return results

def dfs(self, nums, currIndex, currSubset, k, target, resutls):
if len(currSubset) == k and sum(currSubset) == target:
resutls.append(list(currSubset))

for i in range(currIndex, len(nums)):
currSubset.append(nums[i])
self.dfs(nums, i+1, currSubset, k, target, resutls)
currSubset.pop()

```
60 changes: 60 additions & 0 deletions leetcode/377.combination_sum4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
377. Combination Sum IV

中等

https://leetcode.cn/problems/combination-sum-iv/



Given an array of distinct integers nums and a target integer target, return the number of possible combinations that add up to target.

The test cases are generated so that the answer can fit in a 32-bit integer.


```
Example 1:
Input: nums = [1,2,3], target = 4
Output: 7
Explanation:
The possible combination ways are:
(1, 1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 3)
(2, 1, 1)
(2, 2)
(3, 1)
Note that different sequences are counted as different combinations.
Example 2:
Input: nums = [9], target = 3
Output: 0
```

Constraints:
```
1 <= nums.length <= 200
1 <= nums[i] <= 1000
All the elements of nums are unique.
1 <= target <= 1000
```

- Follow up: What if negative numbers are allowed in the given array? How does it change the problem? What limitation we need to add to the question to allow negative numbers?


相关企业

- 亚马逊 Amazon|4
- Facebook|2
- 微软 Microsoft|2

相关标签
- Array
- Dynamic Programming

相似题目
- Combination Sum
中等

8 changes: 5 additions & 3 deletions leetcode/39.combination_sum.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,14 @@ python 语法 之 list的“+“
def dfs(self, candidatesSorted, currIndex, currSubset, results, remainTarget):
if remainTarget == 0:
results.append(currSubset) # 如果用+则这里不需hard copy
return
if remainTarget < 0:
return

for i in range(currIndex, len(candidatesSorted)):
if remainTarget < candidatesSorted[i]:
break
currSubset.append(candidatesSorted[i])
return
self.dfs(candidatesSorted, i,
currSubset + candidatesSorted[i], # 可以用+但是不推荐,因为+实际是复制成一个新的list,时间为O(len(list))
currSubset + [candidatesSorted[i]], # 可以用+但是不推荐,因为+实际是复制成一个新的list,时间为O(len(list))
results, remainTarget - candidatesSorted[i])
```
95 changes: 95 additions & 0 deletions leetcode/40.combination_sum_ii.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
40. Combination Sum II

中等

https://leetcode.cn/problems/combination-sum-ii/



Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum to target.

Each number in candidates may only be used once in the combination.

Note: The solution set must not contain duplicate combinations.


```
Example 1:
Input: candidates = [10,1,2,7,6,1,5], target = 8
Output:
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]
Example 2:
Input: candidates = [2,5,2,1,2], target = 5
Output:
[
[1,2,2],
[5]
]
```


Constraints:
```
1 <= candidates.length <= 100
1 <= candidates[i] <= 50
1 <= target <= 30
```

相关企业

- Facebook|5
- 亚马逊 Amazon|5
- 字节跳动|3
- 彭博 Bloomberg|3
- 微软 Microsoft|2
- 甲骨文 Oracle|2

相关标签
- Array
- Backtracking

相似题目
- Combination Sum
中等

# sol

```py
class Solution:
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
if not candidates:
return []

candidatesSorted = sorted(candidates)
results = []
self.dfs(candidatesSorted, target, 0, [], results)
return results

def dfs(self, candidatesSorted, remainTarget, currIndex, currSubset, results):
if remainTarget < 0:
return
if remainTarget == 0:
results.append(list(currSubset))
return

for i in range(currIndex, len(candidatesSorted)):
if i != currIndex and candidatesSorted[i] == candidatesSorted[i-1]:
continue # 选代表 skip duplicate elements

if remainTarget < candidatesSorted[i]:
break

currSubset.append(candidatesSorted[i])
# each elem used only once so i+1
self.dfs(candidatesSorted, remainTarget - candidatesSorted[i], i + 1, currSubset, results)
currSubset.pop()
```
25 changes: 25 additions & 0 deletions leetcode/78.subset.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,28 @@ layer by layer
[1, 2, 3]
```

```py
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
if not nums:
return []

myqueue = collections.deque([[num] for num in nums])
results = list()
results.append([])
# set().add(())=> (()); cannot use results = set(set()) => ()
# we choose list instead of set because set cannot have set elements

#bfs
while myqueue:
currsubset = myqueue.popleft()
if currsubset not in results:
results.append(list(currsubset))

for i in range(nums.index(currsubset[-1])+1, len(nums)):
tempsubset = list(currsubset) # need deep copy for this loop-layer use only
tempsubset.append(nums[i])
myqueue.append(tempsubset)

return results
```
Loading

0 comments on commit 5241d58

Please sign in to comment.