一串首尾相连的珠子(m 个),有N 种颜色(N<=10),设计一个算法,取出其中一段,要求包含所有N 中颜色,并使长度最短。并分析时间复杂度与空间复杂度。
2014-10-05 14:58
1126 查看
思路:先从index=0处搜索,每检查一颗珠子,响应的颜色数量+1,如果是新的颜色则总颜色数+1.
当颜色总数为n时,找到第一个满足条件的连续序列。
1>从该序列起始处搜索,若搜索处的颜色数量不为1,则表明该串还有别的珠子有该颜色,继续往前搜索并更新该序列,起始索引位置+1.
若搜索处颜色数量为1,停止搜索。
2>比较最佳序列长与当前序列长,更新最佳序列。
4000
记录当前序列起始位置。
从第一个满足条件的序列继续index++,并检查1,2条件。
如果不是首尾相连的:
[cpp] view
plaincopy
#include <iostream>
using namespace std;
#define MAXN 10
int colors[MAXN];//record the counter of one color
int colorsCounter;
void find(int arr[],int len, int colorsNeed)
{
int bestStartIndex = 0;
int bestLen = len;
int lastStartIndex = 0;
for ( int i=0; i<len; ++i)
{
if (!colors[arr[i]])//如果当前数字还没有被记录,则说明又找到一个新的
colorsCounter++;
colors[arr[i]]++;
if (colorsCounter==colorsNeed) //需要的个数已经查找够了
{
int j = lastStartIndex;
while (colors[arr[j]]>1) //直到当前 元素arr 在以后的子序列中不再出现
{
colors[arr[j]]--;
++j; //查找下一个arr[]颜色,看是否以后还存在
}
if (i-j+1<bestLen) //当前子序列小于当前的最佳长度
{
bestStartIndex = j;//记录开始位置
bestLen = i-j+1; //记录最佳长度
if (bestLen==colorsNeed)//如果正好等于需要颜色数,则说明每一个正好一个。则退出
break;
}
lastStartIndex = j;//记录本次开始的位置
}
}
cout << "bestStartIndex: " <<bestStartIndex<< endl;
cout << "bestLen: " <<bestLen<< endl;
for (int i=bestStartIndex; i<bestLen+bestStartIndex; ++i)
cout << arr[i] << " ";
cout << endl;
}
int main()
{
int arr[] = {1,2,3,3,2,1,4,1,3,4,5,5,6,2,3,4,4,1,5,2,3,4};
int m = sizeof(arr)/sizeof(arr[0]);
for ( int i=0; i<m; ++i)
cout << arr[i] <<" ";
cout << endl;
int n = 6;
find(arr,m,n);
return 0;
}
如果带环的话:
再多加一次循环,从上一次得到最小长度的地方开始,到开始阶段,第一次满足所有元素的地方。
[cpp] view
plaincopy
#include <iostream>
using namespace std;
#define MAXN 10
int colors[MAXN];//record the counter of one color
int colorsCounter;
void find(int arr[],int len, int colorsNeed)
{
int bestStartIndex = 0;
int bestLen = len;
int lastStartIndex = 0;
int firstFoundEnoughPearls = len;
/*这里求的是没有绕过环的最小子串长度*/
for ( int i=0; i<len; ++i)
{
if (!colors[arr[i]])
colorsCounter++;
colors[arr[i]]++;
if (colorsCounter==colorsNeed)
{
firstFoundEnoughPearls = i;
int j = lastStartIndex;
while (colors[arr[j]]>1)
{
colors[arr[j]]--;
++j;
}
if (i-j+1<bestLen)
{
bestStartIndex = j;
bestLen = i-j+1;
if (bestLen==colorsNeed)
break;
}
lastStartIndex = j;
}
}
/*这里求的是绕过尾部的子串,然后到当前开始地方的递归*/
for (int i=0; i<firstFoundEnoughPearls; ++i)
{
if (!colors[arr[i]])
colorsCounter++;
colors[arr[i]]++;
int j = lastStartIndex;
while (colors[arr[j]]>1)
{
colors[arr[j]]--;
++j;
}
if (i-j+1+len<bestLen)
{
bestStartIndex = j;
bestLen = i-j+1+len;
if (bestLen==colorsNeed)
break;
}
lastStartIndex = j;
}
int offset = bestStartIndex;
cout<<"bestStartIndex = "<<bestStartIndex<<endl;
for (int i=0; i<bestLen;)
{
cout << arr[i+offset] << " ";
++i;
if (i+offset>=len)
offset = 0-i;
}
cout << endl;
}
int main()
{
int arr[] = {1,2,3,3,2,1,4,1,2,3,2,6,4,5,2,6,2,3,4,1,2,5,2,3,4,5,6};
int m = sizeof(arr)/sizeof(arr[0]);
int n = 6;
find(arr,m,n);
return 0;
}
当颜色总数为n时,找到第一个满足条件的连续序列。
1>从该序列起始处搜索,若搜索处的颜色数量不为1,则表明该串还有别的珠子有该颜色,继续往前搜索并更新该序列,起始索引位置+1.
若搜索处颜色数量为1,停止搜索。
2>比较最佳序列长与当前序列长,更新最佳序列。
4000
记录当前序列起始位置。
从第一个满足条件的序列继续index++,并检查1,2条件。
如果不是首尾相连的:
[cpp] view
plaincopy
#include <iostream>
using namespace std;
#define MAXN 10
int colors[MAXN];//record the counter of one color
int colorsCounter;
void find(int arr[],int len, int colorsNeed)
{
int bestStartIndex = 0;
int bestLen = len;
int lastStartIndex = 0;
for ( int i=0; i<len; ++i)
{
if (!colors[arr[i]])//如果当前数字还没有被记录,则说明又找到一个新的
colorsCounter++;
colors[arr[i]]++;
if (colorsCounter==colorsNeed) //需要的个数已经查找够了
{
int j = lastStartIndex;
while (colors[arr[j]]>1) //直到当前 元素arr 在以后的子序列中不再出现
{
colors[arr[j]]--;
++j; //查找下一个arr[]颜色,看是否以后还存在
}
if (i-j+1<bestLen) //当前子序列小于当前的最佳长度
{
bestStartIndex = j;//记录开始位置
bestLen = i-j+1; //记录最佳长度
if (bestLen==colorsNeed)//如果正好等于需要颜色数,则说明每一个正好一个。则退出
break;
}
lastStartIndex = j;//记录本次开始的位置
}
}
cout << "bestStartIndex: " <<bestStartIndex<< endl;
cout << "bestLen: " <<bestLen<< endl;
for (int i=bestStartIndex; i<bestLen+bestStartIndex; ++i)
cout << arr[i] << " ";
cout << endl;
}
int main()
{
int arr[] = {1,2,3,3,2,1,4,1,3,4,5,5,6,2,3,4,4,1,5,2,3,4};
int m = sizeof(arr)/sizeof(arr[0]);
for ( int i=0; i<m; ++i)
cout << arr[i] <<" ";
cout << endl;
int n = 6;
find(arr,m,n);
return 0;
}
如果带环的话:
再多加一次循环,从上一次得到最小长度的地方开始,到开始阶段,第一次满足所有元素的地方。
[cpp] view
plaincopy
#include <iostream>
using namespace std;
#define MAXN 10
int colors[MAXN];//record the counter of one color
int colorsCounter;
void find(int arr[],int len, int colorsNeed)
{
int bestStartIndex = 0;
int bestLen = len;
int lastStartIndex = 0;
int firstFoundEnoughPearls = len;
/*这里求的是没有绕过环的最小子串长度*/
for ( int i=0; i<len; ++i)
{
if (!colors[arr[i]])
colorsCounter++;
colors[arr[i]]++;
if (colorsCounter==colorsNeed)
{
firstFoundEnoughPearls = i;
int j = lastStartIndex;
while (colors[arr[j]]>1)
{
colors[arr[j]]--;
++j;
}
if (i-j+1<bestLen)
{
bestStartIndex = j;
bestLen = i-j+1;
if (bestLen==colorsNeed)
break;
}
lastStartIndex = j;
}
}
/*这里求的是绕过尾部的子串,然后到当前开始地方的递归*/
for (int i=0; i<firstFoundEnoughPearls; ++i)
{
if (!colors[arr[i]])
colorsCounter++;
colors[arr[i]]++;
int j = lastStartIndex;
while (colors[arr[j]]>1)
{
colors[arr[j]]--;
++j;
}
if (i-j+1+len<bestLen)
{
bestStartIndex = j;
bestLen = i-j+1+len;
if (bestLen==colorsNeed)
break;
}
lastStartIndex = j;
}
int offset = bestStartIndex;
cout<<"bestStartIndex = "<<bestStartIndex<<endl;
for (int i=0; i<bestLen;)
{
cout << arr[i+offset] << " ";
++i;
if (i+offset>=len)
offset = 0-i;
}
cout << endl;
}
int main()
{
int arr[] = {1,2,3,3,2,1,4,1,2,3,2,6,4,5,2,6,2,3,4,1,2,5,2,3,4,5,6};
int m = sizeof(arr)/sizeof(arr[0]);
int n = 6;
find(arr,m,n);
return 0;
}
相关文章推荐
- 一串首尾相连的珠子(m 个),有N 种颜色(N<=10),设计一个算法,取出其中一段,要求包含所有N 中颜色,并使长度最短。并分析时间复杂度与空间复杂度。
- 一串首尾相连的珠子(m个),有N种颜色(N《=10),设计一个算法,取出其中一段,要求包含所有N中颜色,并使长度最短。并分析时间复杂度与空间复杂度
- 一串首尾相连的珠子(m个),有N种颜色(N《=10),设计一个算法,取出其中一段,要求包含所有N中颜色,并使长度最短。并分析时间复杂度与空间复杂度。
- 一串首尾相连的珠子(m个),有N种颜色(N《=10),设计一个算法,取出其中一段,要求包含所有N中颜色,并使长度最短。并分析时间复杂度与空间复杂度。
- 一串首尾相连的珠子(m个),有N种颜色(N<=10),设计一个算法,取出其中一段,要求包含所有N中颜色,并使长度最短
- 一串首尾相连的珠子(m 个),有N 种颜色(N<=10),设计一个算法,取出其中一段,要求包含所有N 中颜色,并使长度最短。
- 一串首尾相连的珠子(m 个),有N 种颜色(N<=10), 设计一个算法,取出其中一段,要求包含所有N 中颜色,并使长度最短
- 字符串匹配之通配符问题------一串首尾相连的珠子(m个),有N种颜色(N《=10),设计一个算法,取出其中一段,要求包含所有N中颜色,并使长度最短。
- 字符串匹配之通配符问题------一串首尾相连的珠子(m个),有N种颜色(N《=10),设计一个算法,取出其中一段,要求包含所有N中颜色,并使长度最短。
- 一串首尾相连的珠子(m个),有N种颜色(N<=10),取出其中一段,要求包含所有N中颜色,并使长度最短
- 【编程题目】一串首尾相连的珠子(m 个),有 N 种颜色(N<=10),取出其中一段,要求包含所有 N 中颜色,并使长度最短。
- 一串首尾相连的珠子(m个),有n种颜色(n<=10) 设计一种算法去除其中一段,要求包含所有的N种颜色
- 40_2 取出首尾相连的珠子中一段,要求包含所有N颜色,并长度最短。 滑动窗口问题
- 一串首尾相连的珠子(m个),有N种颜色(N<=10),使得最短的字符串包含全部颜色
- 长度为n的整数数组,找出其中任意(n-1)个乘积最大的那一组,只能用乘法,不可 以用除法。要求对算法的时间复杂度和空间复杂度作出分析,可以写思路也可以写程序。
- 长度为n的顺序表L,编写一个时间复杂度为O(n),空间复杂度为O(1)的算法,该算法删除线性表中所有值为X的元素
- 给一个数组[a1,a2,a3....an],要求出令ai-aj有最大值,其中i<=j,时间和空间复杂度尽可能小
- 《数据结构》2.10设计一个算法,删除顺序表中值为item的元素,要求算法的时间复杂度是O(n),空间复杂度是O(1)
- 有一串首尾相连的珠子,共有m个,每一个珠子有一种颜色,并且颜色的总数不超过n(n<=10),求连续的珠子的颜色总数为n时,长度最小的区间。
- 一串首尾相连的珠子共m个,其中有n中颜色,求出包含全部这n种颜色的最短的串。