[英]Minimize or find a n ideal subset-sums from a list of numbers which are less than or equal to some N
Given a list of numbers, lst = [1, 1, 2, 4, 3, 2, 1, 1, 1, 2, 1, 4, 3, 1]
how would I find an ideal number of lists which are less than or equal to 4?给定一个数字列表, lst = [1, 1, 2, 4, 3, 2, 1, 1, 1, 2, 1, 4, 3, 1]
我将如何找到小于或等于4?
There are many possibilities here.这里有很多可能性。 The goal is to minimize the number of possible lists.目标是最小化可能列表的数量。 The program would need to do create subset lists like the following: {4}, {4}, {3, 1}, ... , {1, 1}
.该程序需要创建如下所示的子集列表: {4}, {4}, {3, 1}, ... , {1, 1}
。
Notice how the last list subset does not equal to four, but it less.请注意最后一个列表子集如何不等于 4,而是小于 4。 This problem is difficult for the following reasons:这个问题很困难,原因如下:
subset-sums
which are less than or equal to a sum程序必须能够找到小于或等于总和的subset-sums
总和Here's my attempt.这是我的尝试。 The general idea is to sort the list, and go from left and right and picking the biggest subset every iteration greedily.一般的想法是对列表进行排序,从左到右,每次迭代都贪婪地选择最大的子集。 The time complexity is O(n)
.时间复杂度是O(n)
。
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> lst{1, 1, 2, 4, 3, 2, 1, 1, 1, 2, 1, 4, 3, 1};
std::sort(lst.begin(),lst.end()); //sort the list
int target = 4;
int left = 0;
int right = lst.size()-1;
std::vector<std::vector<int>> solutions;
while (left<right ){
if(lst[left] > target) // break if no solutions
break;
if(lst[right] > target) // ignore larger right values
right--;
if(lst[right]<=target){ // while the total sum is less than target, keep adding the elements
std::vector<int> subset;
subset.push_back(lst[right]);
int sum = lst[right];
while(left<right && lst[left]+sum<=target){
sum+=lst[left];
subset.push_back({lst[left]});
left++;
}
solutions.push_back(subset);
right--;
}
}
for(auto& ss : solutions){
std::cout<<'{';
for(auto n:ss){
std::cout<<n<<',';
}
std::cout<<"\b}";
std::cout<<std::endl;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.