求珠子的長度最小區(qū)間

有一串首尾相連的珠子,共有m個,每一個珠子有一種顏色,并且顏色的總數(shù)不超過n(n<=10),求連續(xù)的珠子的顏色總數(shù)為n時,長度最小的區(qū)間。

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

網(wǎng)頁題目:求珠子的長度最小區(qū)間
URL網(wǎng)址:http://muchs.cn/article30/gdsgso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計公司動態(tài)網(wǎng)站、靜態(tài)網(wǎng)站商城網(wǎng)站、網(wǎng)站建設(shè)ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)