有一串首尾相连的珠子,共有m个,每一个珠子有一种颜色,并且颜色的总数不超过n(n<=10),求连续的珠子的颜色总数为n时,长度最小的区间。

#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) {
                colors[arr[j]]--;
                ++j;
            }
            if (i-j+1<bestLen) {
                bestStartIndex = j;
                bestLen = i-j+1;
                if (bestLen==colorsNeed)
                    break;
            }
            lastStartIndex = j;
        }
    }