求珠子的长度最小区间

有一串首尾相连的珠子,共有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;
        }
    }
时间: 2024-08-25 10:38:25

求珠子的长度最小区间的相关文章

poj 1961 Period【求前缀的长度,以及其中最小循环节的循环次数】

Period Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 14653   Accepted: 6965 Description For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want to know whether the

bfs:求最短路径的长度

*/--> pre.src {background-color: Black; color: White;} pre.src {background-color: Black; color: White;} pre.src {background-color: Black; color: White;} pre.src {background-color: Black; color: White;} pre.src {background-color: Black; color: White;}

大于或等于给定值 长度最小的子数组 minimum size subarray sum [209]

leetcode 209 Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7, the subarray [4,3] ha

求给定数据中最小的K个数

public class MinHeap { /* * * Top K个问题,求给定数据中最小的K个数 * * 最小堆解决:堆顶元素为堆中最大元素 * * * */ private int MAX_DATA = 10;//最小10个数 private int[] data;//存储数据 private int len;//当前存储长度,考虑到元素个数可能没有10个,这个时候全部输出 private MinHeap() { data = new int[MAX_DATA]; len=0; } pr

笔试题: 不使用中间变量求const字符串长度,即实现求字符串长度库函数strlen函数

笔试题: 不使用中间变量求const字符串长度,即实现求字符串长度库函数strlen函数. 函数接口声明如下:int my_strlen(const char *p); strlen函数实际完成的功能是从代表该字符串的第一个地址开始遍历,直到遇到结束符'\0'. 而返回的长度大小不包括'\0'. #include <stdio.h> #include <assert.h> //使用中间变量 //int my_strlen(const  char *str) //{ //   ass

HDU 4738——Caocao&#39;s Bridges——————【求割边/桥的最小权值】

Caocao's Bridges Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4738 Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn't give up. Caocao's army st

转:最小区间:k个有序的数组,找到最小区间使k个数组中每个数组至少有一个数在区间中

转:http://www.itmian4.com/thread-6504-1-1.html 最小区间原题 k个有序的数组,找到最小的区间范围使得这k个数组中,每个数组至少有一个数字在这个区间范围内.比如: 数组1:[4, 10, 15, 24, 26] 数组2:[0, 9, 12, 20] 数组3:[5, 18, 22, 30] 最小的区间是[20, 24],这个区间包含了数组1中的24,数组2中的20,数组3中的22 思考时间~~~ 分析 该题看起来还算比较简单,大家通常都会想到:为每一个数组

【C语言】求旋转数组的最小数字,输入一个递增排序的数组的一个旋转,输出其最小元素

//求旋转数组的最小数字,输入一个递增排序的数组的一个旋转,输出其最小元素 #include <stdio.h> #include <string.h> int find_min(int arr[],int len) { int i = 0; for (i = 1; i < len; i++) { if (arr[i] < arr[0]) return arr[i]; } return arr[0]; } int main() { int i; int arr1[] =

程序猿之---C语言细节17(求time_t的最大值、strlen求的是长度、malloc分配字符内存细节、switch的中default细节)

主要内容:求time_t的最大值.strlen求的是长度.malloc分配字符内存细节.switch的中default细节 #include <stdio.h> #include <time.h> int main() {     /*****************************************************************         time_t最大值测试     ************************************