一串首尾相连的珠子(m 个),有N 种颜色(N<=10),设计一个算法,取出其中一段,要求包含所有N 中颜色,并使长度最短。并分析时间复杂度与空间复杂度。

思路:先从index=0处搜索,每检查一颗珠子,响应的颜色数量+1,如果是新的颜色则总颜色数+1.

                     当颜色总数为n时,找到第一个满足条件的连续序列。

                           1>从该序列起始处搜索,若搜索处的颜色数量不为1,则表明该串还有别的珠子有该颜色,继续往前搜索并更新该序列,起始索引位置+1.

                               若搜索处颜色数量为1,停止搜索。

                            2>比较最佳序列长与当前序列长,更新最佳序列。记录当前序列起始位置。

                      从第一个满足条件的序列继续index++,并检查1,2条件。

          如果不是首尾相连的:

#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;
}


 

如果带环的话:

       再多加一次循环,从上一次得到最小长度的地方开始,到开始阶段,第一次满足所有元素的地方。

#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;
}


 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值