ZhongKuo0228 / study

0 stars 0 forks source link

39. Combination Sum #29

Open fockspaces opened 1 year ago

fockspaces commented 1 year ago

Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order.

The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different.

The test cases are generated such that the number of unique combinations that sum up to target is less than 150 combinations for the given input.

Example 1:

Input: candidates = [2,3,6,7], target = 7 Output: [[2,2,3],[7]] Explanation: 2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times. 7 is a candidate, and 7 = 7. These are the only two combinations. Example 2:

Input: candidates = [2,3,5], target = 8 Output: [[2,2,2,2],[2,3,3],[3,5]] Example 3:

Input: candidates = [2], target = 1 Output: []

fockspaces commented 1 year ago

解法一:recursion 每次都是拿 or 不拿,把每個分支都走過之後,就能窮舉出所有組合

class Solution {
public:
    vector<vector<int>> ans;
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        vector<int> list;
        DFS(candidates, target, 0, list);
        return ans;
    }

    void DFS(vector<int>& candidates, int cur, int index, vector<int>& list) {
        if(cur < 0) return;
        if(cur == 0) return ans.push_back(list);

        for(int i = index; i < candidates.size(); i++) {
            list.push_back(candidates[i]);
            DFS(candidates, cur - candidates[i], i, list);
            list.pop_back();
        }
    }
};
fockspaces commented 1 year ago

因為是求 (1) 所有組合 (2) 可重複值 感覺不好用DP來解

fockspaces commented 1 year ago
  1. 要特別注意 list 傳參會 pass by ref,最後再進 ans 仍會動態被修改 應該用 .copy() 來丟進 ans
  2. 注意 self 用法
class Solution:
    def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
        self.ans = []
        self.helper(candidates, target, 0, [], 0)
        return self.ans

    def helper(self, candidates, target, cur, list, idx):
        if cur == target:
            return self.ans.append(list.copy())
        if cur > target:
            return
        for i in range(idx, len(candidates)):
            list.append(candidates[i])
            self.helper(candidates, target, cur + candidates[i], list, i)
            list.pop()
fockspaces commented 1 year ago

可以細品這個寫法,省去 self,並用 recursion 來區分 加入當前 與 不加入 的選擇

class Solution:
    def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
        def helper(target, index, path):
            if target == 0:
                return [path]
            if target < 0 or index == len(candidates):
                return []
            return helper(target - candidates[index], index, path + [candidates[index]]) + helper(target, index + 1, path)
        return helper(target, 0, [])
fockspaces commented 1 year ago
class Solution:
    def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
        def dfs(target, idx, path):
            if target == 0:
                return [path]
            if idx == len(candidates) or target < 0:
                return []
            return dfs(target - candidates[idx], idx, [*path, candidates[idx]]) + dfs(target, idx + 1, path) 
        return dfs(target, 0, [])