From 5241d5862f27f17aa85b36fd7873566e4d1d3933 Mon Sep 17 00:00:00 2001 From: BigCircle Date: Fri, 3 Jun 2022 17:08:15 -0400 Subject: [PATCH] 78 subsets bfs --- README.md | 13 +-- ...7.Letter_Combinations_of_a_Phone_Number.md | 4 +- leetcode/216.combination_sum3.md | 84 ++++++++++++++++ leetcode/377.combination_sum4.md | 60 ++++++++++++ leetcode/39.combination_sum.md | 8 +- leetcode/40.combination_sum_ii.md | 95 +++++++++++++++++++ leetcode/78.subset.md | 25 +++++ leetcode/79.word_search.md | 67 +++++++++++++ 8 files changed, 346 insertions(+), 10 deletions(-) create mode 100644 leetcode/216.combination_sum3.md create mode 100644 leetcode/377.combination_sum4.md create mode 100644 leetcode/40.combination_sum_ii.md create mode 100644 leetcode/79.word_search.md diff --git a/README.md b/README.md index 675a499..b3bcf80 100644 --- a/README.md +++ b/README.md @@ -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 双向 @@ -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 diff --git a/leetcode/17.Letter_Combinations_of_a_Phone_Number.md b/leetcode/17.Letter_Combinations_of_a_Phone_Number.md index 38e9062..5d21783 100644 --- a/leetcode/17.Letter_Combinations_of_a_Phone_Number.md +++ b/leetcode/17.Letter_Combinations_of_a_Phone_Number.md @@ -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. @@ -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 ``` \ No newline at end of file diff --git a/leetcode/216.combination_sum3.md b/leetcode/216.combination_sum3.md new file mode 100644 index 0000000..bc89b72 --- /dev/null +++ b/leetcode/216.combination_sum3.md @@ -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() + +``` \ No newline at end of file diff --git a/leetcode/377.combination_sum4.md b/leetcode/377.combination_sum4.md new file mode 100644 index 0000000..61883ed --- /dev/null +++ b/leetcode/377.combination_sum4.md @@ -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 +中等 + diff --git a/leetcode/39.combination_sum.md b/leetcode/39.combination_sum.md index 47b87da..73ee152 100644 --- a/leetcode/39.combination_sum.md +++ b/leetcode/39.combination_sum.md @@ -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]) ``` \ No newline at end of file diff --git a/leetcode/40.combination_sum_ii.md b/leetcode/40.combination_sum_ii.md new file mode 100644 index 0000000..c6d0592 --- /dev/null +++ b/leetcode/40.combination_sum_ii.md @@ -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() +``` \ No newline at end of file diff --git a/leetcode/78.subset.md b/leetcode/78.subset.md index 74af523..963c918 100644 --- a/leetcode/78.subset.md +++ b/leetcode/78.subset.md @@ -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 +``` \ No newline at end of file diff --git a/leetcode/79.word_search.md b/leetcode/79.word_search.md new file mode 100644 index 0000000..a3288b2 --- /dev/null +++ b/leetcode/79.word_search.md @@ -0,0 +1,67 @@ +79. Word Search + +中等 + +https://leetcode.cn/problems/word-search/ + + +Given an m x n grid of characters board and a string word, return true if word exists in the grid. + +The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once. + + +``` +Example 1: + + +Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED" +Output: true + +Example 2: + + +Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE" +Output: true + +Example 3: + + +Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB" +Output: false +``` + +Constraints: +``` +m == board.length +n = board[i].length +1 <= m, n <= 6 +1 <= word.length <= 15 +board and word consists of only lowercase and uppercase English letters. +``` + + +Follow up: Could you use search pruning to make your solution faster with a larger board? + + +相关企业 + +- 亚马逊 Amazon|34 +- 微软 Microsoft|23 +- 优步 Uber|17 +- 彭博 Bloomberg|16 +- Indeed|11 +- Facebook|10 +- 字节跳动|7 +- 思科 Cisco|6 +- 苹果 Apple|5 +- 谷歌 Google|3 + +相关标签 +- Array +- Backtracking +- Matrix + +相似题目 +- Word Search II +困难 +