爨爨爨好

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

▶ 给定一个数组 和一个目标值。从该数组中选出若干项(项数不定),使他们的和等于目标值。

▶ 36. 数组元素无重复

● 代码,初版,19 ms 。从底向上的动态规划,但是转移方程比较智障(将待求数分解为左右两个半段,分别找解,拼在一起,再在接缝上检查是否是重复解)。

 1 class Solution
 2 {
 3 public:
 4     vector<vector<int>> combinationSum(vector<int>& candidates, int target)
 5     {
 6         sort(candidates.begin(), candidates.end());
 7         int n, i, j, k;
 8         vector<int> temp;
 9         vector<int>& cand = candidates;
10         vector<vector<int>> left, right;
11         vector<vector<vector<int>>> table(target - cand[0] + 1);
12         unordered_map<int, int> map;
13 
14         if (target < cand[0])
15             return vector<vector<int>>{};
16         for (i = 0; i < candidates.size(); i++)
17             map.emplace(candidates[i], i);
18         for (n = cand[0]; n <= target; n++)                         // 每次循环计算一个数 n
19         {
20             if (map.find(n) != map.end())                           // 原数组中有 n,本身就是一个解
21                 table[n - cand[0]].push_back(vector<int>{n});
22             if (!(n % 2) && map.find(n / 2) != map.end())           // 将偶数 n 分拆为两个 n/2 的和的解
23                 table[n - cand[0]].push_back(vector<int>{n/2, n/2});
24             for (i = cand[0]; i <= (n - 1) / 2; i++)                // 将 n 分解为左右两个半段,将各自的解进行笛卡尔积
25             {
26                 left = table[i - cand[0]];
27                 right = table[n - i - cand[0]];
28                 if (left.size() == 0 || right.size() == 0)          // 左右半段至少一个为空,不可照此分解
29                     continue;
30                 for (j = 0; j < left.size(); j++)
31                 {
32                     for (k = 0; k < right.size(); k++)
33                     {
34                         if (left[j][left[j].size() - 1] > right[k][0] ||
35                             left[j].size() > 1 && table[i - left[j][left[j].size() - 1] - cand[0]].size() > 0 && table[n - i + left[j][left[j].size() - 1] - cand[0]].size() > 0)
36                             continue;// 删除解的条件:左半段最后一个元素比右半段第一个元素大(不保持单调);将左半段最后一个元素转移给右半段后仍是一个解(该解一定在之前的搜索中出现过)
37                         temp = left[j];
38                         temp.insert(temp.end(), right[k].begin(), right[k].end());
39                         table[n - cand[0]].push_back(temp);
40                     }
41                 }
42             }
43         }
44         return table[target - cand[0]];
45     }
46 };

● 代码,成套方法系列,13 ms 。从顶向下的递归,向解向量集中逐一添加元素。最快的解法算法与之相同。

 1 class Solution
 2 {
 3 public:
 4     vector<vector<int> > combinationSum(vector<int> &candidates, int target)
 5     {
 6         sort(candidates.begin(), candidates.end()); 
 7         vector<vector<int>> res; 
 8         vector<int> combination; 
 9         sum(candidates, target, res, combination, 0);
10         return res;
11     }
12     void sum(vector<int> &candidates, int target, vector<vector<int>> &res, vector<int> &combination, int begin)
13     {
14         if (!target)
15         {
16             res.push_back(combination);
17             return;
18         }
19         for (int i = begin; i < candidates.size() && target >= candidates[i]; i++)// 每次向现有组合中添加一项,使用剩下的项来求解一个更小的和
20         {
21             combination.push_back(candidates[i]);
22             sum(candidates, target - candidates[i], res, combination, i);
23             combination.pop_back();
24         }
25     }
26 };

● 代码,大佬的版本,9 ms 。与上面的成套方法基本相同。

 1 class Solution
 2 {
 3 public:
 4     vector<vector<int>> sum(vector<int>& candidates, int m, int target)
 5     {
 6         vector<vector<int>> output, x;
 7         int c, rep;
 8         for (int i = m; i < candidates.size(); i++)
 9         {
10             if (candidates[i] > target)
11                 break;
12             for (c = 1; target - c * candidates[i] >= 0; c++)
13             {
14                 if (target - c * candidates[i] == 0)
15                 {
16                     output.push_back(vector<int>(c, candidates[i]));
17                     break;
18                 }
19                 x = sum(candidates, i + 1, target - c * candidates[i]);
20                 for (auto & k : x)
21                 {
22                     for(rep = c;rep > 0;rep--)
23                         k.push_back(candidates[i]);
24                     output.push_back(k);
25                 }
26             }
27         }
28         return output;
29     }
30     vector<vector<int>> combinationSum(vector<int>& candidates, int target)
31     {
32         sort(candidates.begin(), candidates.end());
33         return sum(candidates, 0, target);
34     }
35 };

 

▶ 39.数组元素有重复(给重复解检查造成困难)

● 代码,成套方法系列,12 ms 。添加一个跳过相同元素的判断。

 1 class Solution
 2 {
 3 public:
 4     vector<vector<int> > combinationSum2(vector<int> &candidates, int target)
 5     {
 6         sort(candidates.begin(), candidates.end());
 7         vector<vector<int>> res;
 8         vector<int> combination;
 9         sum(candidates, target, res, combination, 0);
10         return res;
11     }
12     void sum(vector<int> &candidates, int target, vector<vector<int>> &res, vector<int> &combination, int begin)
13     {
14         if (!target)
15         {
16             res.push_back(combination);
17             return;
18         }
19         for (int i = begin; i < candidates.size() && target >= candidates[i]; i++)// 每次向现有组合中添加一项,使用剩下的项来求解一个更小的和
20         {
21             
22             if (i != begin && candidates[i] == candidates[i - 1])// 跳过相同的元素,否则会出现重复解
23                 continue;
24             combination.push_back(candidates[i]);
25             sum(candidates, target - candidates[i], res, combination, i + 1);
26             combination.pop_back();
27         }
28     }
29 };

 ● 大佬的代码,12 ms,算法与上面相同。

 1 class Solution
 2 {
 3 public:
 4     vector<vector<int>> sum(vector<int>& candidates, int m, int target)
 5     {
 6         vector<vector<int>> output, x;
 7         int i, j, k, c;
 8         for (i = m; i < candidates.size();i++)
 9         {
10             if (candidates[i] > target)// 最小元素过大
11                 break;
12             for (j = i; i < candidates.size() - 1 && candidates[i] == candidates[i + 1]; i++);// 跳过重复元素
13             for (k = 1, c = i - j + 1; k <= c; k++)                     // 尽量多的选取 candidates[j]
14             {
15                 if (k * candidates[j] == target)
16                 {
17                     output.push_back(vector<int>(k, candidates[j]));
18                     break;
19                 }
20                 else if (k * candidates[j] > target)
21                     break;
22                 vector<int> first(k, candidates[j]);                    // 选定 k 个 candidates[j]
23                 x = sum(candidates, i + 1, target - k*candidates[j]);   // 剩下的工作由递归完成
24                 for (auto& k : x)
25                 {
26                     vector<int> second = first;
27                     for (auto p : k) second.push_back(p);
28                     output.push_back(second);
29                 }
30             }
31         }
32         return output;
33     }
34     vector<vector<int>> combinationSum2(vector<int>& candidates, int target)
35     {
36         sort(candidates.begin(), candidates.end());
37         return sum(candidates, 0, target);
38     }
39 };

 

▶ 216. 没有给出确定的数组,而是指定了加数个数和目标值,且要求各加数不相等。

● 代码,成套方法系列,2 ms 。

 1 class Solution
 2 {
 3 public:
 4     std::vector<std::vector<int> > combinationSum3(int k, int n)
 5     {
 6         std::vector<std::vector<int> > res;
 7         std::vector<int> combination;
 8         sum(n, res, combination, 1, k);
 9         return res;
10     }
11     void sum(int target, std::vector<std::vector<int> > &res, std::vector<int> &combination, int begin, int need)
12     {
13         if (!target)
14         {
15             res.push_back(combination);
16             return;
17         }
18         else if (!need)
19             return;
20         for (int i = begin; i < 10 && target >= i * need + need * (need - 1) / 2; i++)
21             // 要求 need 个数和为 target,最小的加数 i 满足 target >= i * need(不大于平均数),超过 target / need 的选择被剪枝
22             // 剩余 (need - 1) 个数的和最小为 1 + 2 + ... + (need-1) = need * (need - 1) / 2,阻止 i 选择过大
23         {
24             combination.push_back(i);
25             sum(target - i, res, combination, i + 1, need - 1);
26             combination.pop_back();
27         }
28     }
29 };

 ● 大佬的代码,4 ms,算法与上面相同。

 1 class Solution
 2 {
 3 public:
 4     vector<vector<int>> combinationSum3(int k, int n)
 5     {
 6         vector<vector<int>> res;
 7         vector<int> solu;
 8         sum(res, solu, k, n);
 9         return res;
10     }
11     void sum(vector<vector<int>> & res, vector<int> solu, int k, int n) 
12     {
13         if (solu.size() == k && n == 0) 
14             res.push_back(solu);
15         if (solu.size() < k)
16         {
17             for (int i = (solu.size() == 0 ? 1 : solu.back() + 1); i <= 9; i++)
18             {
19                 if (i > n) break;
20                 solu.push_back(i);
21                 sum(res, solu, k, n - i);
22                 solu.pop_back();
23             }
24         }
25     }
26 };

 

▶ 377. 数组元素可以重复使用,要求计算解的个数(包括交换两个不相等元素构成的新解)

● 代码,初版,476 ms 。智障的动态规划,使用两个向量组交替求解从数组最小元素到目标值之间各整数的拆分方法数。

 1 class Solution
 2 {
 3 public:
 4     int combinationSum4(vector<int> &candidates, int target)
 5     {
 6         if (candidates.size() == 0)
 7             return 0;
 8         sort(candidates.begin(), candidates.end());
 9         if (target < candidates[0])
10             return 0;
11         
12         vector<vector<int>> table;
13         table.push_back(vector<int>(target + 1, 0));
14         table.push_back(vector<int>(target + 1, 0));       
15         int i, j, k, count;
16         for (i = count = 0; i < candidates.size() && candidates[i] <= target; i++)
17             table[0][candidates[i]] = 1; 
18         count += table[0][target];                              // candidates 本身包含了 target
19         for (i = 2; i <= (target - 1) / candidates[0] + 1; i++) // 填表,循环次数控制
20         {
21             table[(i + 1) % 2].assign(target + 1, 0);           // 擦除目标行
22             for (j = candidates[0] * i; j <= target; j++)
23             {
24                 for (k = 0; k < candidates.size() && candidates[k] <= target; k++)
25                 {
26                     if (j - candidates[k] >= 0 && j - candidates[k] <= target)
27                         table[(i + 1) % 2][j] += table[i % 2][j - candidates[k]];
28                 }
29             }
30             count += table[(i + 1) % 2][target];
31         }
32         return count;
33     }
34 };

● 大佬的代码,3 ms,正确的动态规划姿势,仅在一个向量中进行递推。

 1 class Solution
 2 {
 3 public:
 4     int combinationSum4(vector<int>& nums, int target)
 5     {
 6         vector<int> t(target + 1, 0);
 7         for (int i = 1; i <= target; i++)
 8         {
 9             for (auto n : nums)
10             {
11                 if (n == i)
12                     t[i]++;
13                 else if (n < i)
14                     t[i] += t[i - n];
15             }
16         }
17         return t[target];
18     }
19 };

 

posted on 2018-01-17 16:25  爨爨爨好  阅读(136)  评论(0编辑  收藏  举报