632. 最小区间
难度困难235收藏分享切换为英文关注反馈
你有 k
个升序排列的整数列表。找到一个最小区间,使得 k
个列表中的每个列表至少有一个数包含在其中。
我们定义如果 b-a < d-c
或者在 b-a == d-c
时 a < c
,则区间 [a,b] 比 [c,d] 小。
示例:
输入:[[4,10,15,24,26], [0,9,12,20], [5,18,22,30]] 输出:[20,24] 解释: 列表 1:[4, 10, 15, 24, 26],24 在区间 [20,24] 中。 列表 2:[0, 9, 12, 20],20 在区间 [20,24] 中。 列表 3:[5, 18, 22, 30],22 在区间 [20,24] 中。
提示:
k
<= 3500元素的值
<= 105通过次数14,960提交次数25,456
这道题用二分,剪剪枝然后循环过一下就好了、
时间复杂度是O(k*n*log(k*n))
#include
#include
#include
#include
#include
using namespace std;
vector::iterator getbinary(vector &v,int n)
{
int nleft = 0;
int nright = v.size() - 1;
int mid = (nleft + nright) / 2;
while (nleft < nright)
{
int Val = *(v.begin() + mid);
if (Val >= n)
{
nright = mid - 1;
}
else if (Val < n)
{
nleft = mid + 1;
}
mid = (nleft + nright) / 2;
}
return v.begin() + nleft;
}
vector::iterator getmaxmin(vector &v, int n)
{
if(*v.begin() == n)
return v.begin();
if (*v.begin() > n)
return v.end();
if (*(v.end() - 1) > n)
{
vector::iterator it = getbinary(v, n);
if (*it > n)
return it-1;
else return it;
}
return v.end()-1;
}
vector::iterator getminmax(vector &v, int n)
{
if (*(v.end() - 1) < n)
return v.end();
if (*v.begin() >= n)
return v.begin();
vector::iterator it = getbinary(v, n);
if (*it >= n)
return it;
else return it + 1;
}
vector smallestRange(vector>& nums) {
int nleft,nright = 0;
int nLen = 200000;
vector nums_max;
vector v_ans;
v_ans.clear();
for (auto it = nums.begin(); it != nums.end(); it++)
{
auto itit = it->begin();
for (itit; itit != it->end(); itit++)
{
nums_max.push_back(*itit);
}
}
sort(nums_max.begin(), nums_max.end());
int num = -100001;
for (auto mit = nums_max.begin(); mit != nums_max.end(); mit++)
{
nright = *mit;
if (nright == num)
{
continue;
}
else
{
num = nright;
}
int nflag = 0;
int nmax = 100001;
for (auto nit = nums.begin(); nit != nums.end(); nit++)
{
vector::iterator rit = getmaxmin(*nit, nright);
if (rit != nit->end())
{
if (nmax > *rit)
nmax = *rit;
}
if (rit == nit->end())
{
nflag = 1;
break;
}
}
if (nflag != 1)
{
nleft = nmax;
if (nright - nleft < nLen)
{
v_ans.clear();
v_ans.push_back(nleft);
v_ans.push_back(nright);
nLen = nright - nleft;
}
}
nleft = *mit;
nflag = 0;
nmax = -100001;
for (auto nit = nums.begin(); nit != nums.end(); nit++)
{
vector::iterator rit = getminmax(*nit, nright);
if (rit != nit->end())
if (nmax < *rit)
nmax = *rit;
if (rit == nit->end())
{
nflag = 1;
break;
}
}
if (nflag)
continue;
nright = nmax;
if (nright - nleft < nLen)
{
v_ans.clear();
v_ans.push_back(nleft);
v_ans.push_back(nright);
nLen = nright - nleft;
}
}
return v_ans;
}
int main()
{
vector> nums;// = [[-89, 1, 69, 89, 90, 98], [-43, -36, -24, -14, 49, 61, 66, 69], [73, 94, 94, 96], [11, 13, 76, 79, 90], [-40, -20, 1, 9, 12, 12, 14], [-91, -31, 0, 21, 25, 26, 28, 29, 29, 30], [23, 88, 89], [31, 42, 42, 57], [-2, 6, 11, 12, 12, 13, 15], [-3, 25, 34, 36, 39], [-7, 3, 29, 29, 31, 32, 33], [4, 11, 14, 15, 15, 18, 19], [-34, 9, 12, 19, 19, 19, 19, 20], [-26, 4, 47, 53, 64, 64, 64, 64, 64, 65], [-51, -25, 36, 38, 50, 54], [17, 25, 38, 38, 38, 38, 40], [-30, 12, 15, 19, 19, 20, 22], [-14, -13, -10, 68, 69, 69, 72, 74, 75], [-39, 42, 70, 70, 70, 71, 72, 72, 73], [-67, -34, 6, 26, 28, 28, 28, 28, 29, 30, 31]];
vectorv;
/* v.push_back(4);
v.push_back(10);
v.push_back(15);
v.push_back(24);
v.push_back(26);
nums.push_back(v);
v.clear();
v.push_back(0);
v.push_back(9);
v.push_back(12);
v.push_back(20);
nums.push_back(v);
v.clear();
v.push_back(5);
v.push_back(18);
v.push_back(22);
v.push_back(30);
nums.push_back(v);*/
/* v.push_back(-5);
v.push_back(-4);
v.push_back(-3);
v.push_back(-2);
v.push_back(-1);
v.push_back(0);
nums.push_back(v);
v.clear();
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
nums.push_back(v);
v.clear();
v.push_back(5);
v.push_back(18);
v.push_back(22);
v.push_back(30);
nums.push_back(v);*/
/*v.push_back(10);
v.push_back(10);
nums.push_back(v);
v.clear();
v.push_back(11);
v.push_back(11);
nums.push_back(v);*/
/* int a[13] = { 11,14,8,11,8,21,15,20,15,24,19,17,12};
for (int i = 0; i < 13; i++)
{
int n;
for (int j = 0; j < a[i]; j++)
{
scanf("%d", &n);
v.push_back(n);
}
nums.push_back(v);
v.clear();
}*/
v.push_back(1);
v.push_back(2);
v.push_back(3);
nums.push_back(v);
v.clear();
v.push_back(1);
v.push_back(2);
v.push_back(3);
nums.push_back(v);
v.clear();
v.push_back(1);
v.push_back(2);
v.push_back(3);
nums.push_back(v);
vector v_ans = smallestRange(nums);
auto it = v_ans.begin();
for (it; it != v_ans.end(); it++)
{
cout << *it << endl;
}
return 0;
}