-
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.
- Loading branch information
Showing
8 changed files
with
346 additions
and
10 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
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
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,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() | ||
|
||
``` |
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,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 | ||
中等 | ||
|
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
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,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() | ||
``` |
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
Oops, something went wrong.