标签:tor solution == 个数 size minimum blank ble ems
题意:将n个数分成k组,使得k组中的最大值最小。
题解:暴力DFS,但是要注意两个地方剪枝,首先在DFS的过程中判断当前的最大值是不是已经超过了已有答案。
第二个剪枝的地方比较triky,由于我们对k组没有顺序要求的,所以当剩下的组都是空的时候,我们只需要DFS第一个组。
class Solution {
public:
int p[15];
int ans;
int n;
int minimumTimeRequired(vector<int>& jobs, int k) {
n = jobs.size();
ans=INT_MAX;
fun(0,k,0,jobs);
return ans;
}
void fun(int i, int k, int m, vector<int>& jobs)
{
if (i == n)
{
if (ans > m)
ans = m;
return;
}
for (int j = 0; j < k; j++)
{
p[j] += jobs[i];
if (p[j] > ans)
{
p[j] -= jobs[i];
continue;
}
if (p[j] > m)
fun(i + 1, k, p[j], jobs);
else
fun(i + 1, k, m, jobs);
p[j] -= jobs[i];
if(p[j]==0)
break;
}
}
};
LeetCode 1723. Find Minimum Time to Finish All Jobs
标签:tor solution == 个数 size minimum blank ble ems
原文地址:https://www.cnblogs.com/dacc123/p/14358725.html