M beads in N colors to find the shortest continuous fragment containing N colors

Source: Internet
Author: User

Original question:

There is a string of beads (the beginning and the end are not connected), a total of m, each bead has a color, and the total number of colors does not exceed n (n <= 10 ), when the total number of consecutive beads is N, the minimum length interval is obtained.

Question Analysis:

I. Brute Force Search

1. The simplest method is brute force search. Scanning the nth position one by one starts to contain the shortest color range of n. the time complexity is O (M ^ 2 ).

2. Start scanning from I. Each time a new color appears, the count is + 1. When the count is N, it ends. This is the shortest interval starting with I.

II,

1. Scan the array m and calculate the next occurrence of each color in the array M, in data nextcolor [m] (the next position of the last element of each color is-1.Program).

2. Scan the array m from the 0 position to find the first interval containing all colors. the start position is begintag (0 at this time), and the end position is recorded as endtag.

3. perform operations on begintag: if the next color of the intag position is nextcolor [begintag] <= endtag, begintag ++ is used to repeat this step, until nextcolor [begintag]> endtag or begintag = endtag, begintag ----> endtag is the minimum interval that ends with an endtag but contains all colors.

4. Move the endtag one step later and repeat Step 3.

5. Repeat Step 4 until endtag = M. Length-1.

6. At this time, we can get the length of the shortest interval at the end of all elements and select the smallest one.

CodeAs follows:

 Package  Ddc.test.com;  Public   Class  Mnselecttestmain {  /**  *  @ Param  ARGs  */      Public   Static   Void Main (string [] ARGs ){  //  Todo auto-generated method stub          Int Data [] = {, }; Getsuball (data, 6 );}  Public   Static   Int [] Getsuball ( Int [] Colorarray, Int  Colorlength ){  For (Int  Color: colorarray ){  If (Color> = Colorlength)  Throw   New Runtimeexception ("Color Error !! " );}  Int [] Least = New   Int  [Colorarray. Length];  Int [] Nextcolor = New   Int [Colorarray. Length];  For ( Int I = 0; I <nextcolor. length; I ++ ) Nextcolor [I] =-1 ;  Int [] Colorcounter = New   Int  [Colorlength];  Int [] Colortmp = New   Int  [Colorlength];  For (Int I = 0; I <colortmp. length; I ++ ) Colortmp [I] =-1 ;  For ( Int I = colorarray. Length-1; I> = 0; I -- ) {Nextcolor [I] = Colortmp [colorarray [I]; colortmp [colorarray [I] = I ;}  Int Hascolor = 0; //  Total number of detected colors          Int Begintag = 0, endtag = 0 ;  //  Find the first end point that contains all colors          While (Endtag < Colorarray. Length ){  If (Colorcounter [colorarray [endtag] = 0 ) {Hascolor ++ ; Colorcounter [colorarray [endtag] ++ ;}  If (Hascolor = Colorlength)  Break ; Endtag ++ ;}  //  Gradually move the end point back, and then find the shortest interval with endtag as the end point          While (Endtag < Colorarray. Length ){  If (Nextcolor [begintag]> = 0 & nextcolor [begintag] <= Endtag) {begintag ++ ;  Continue  ;} Least [endtag] = Endtag-begintag + 1 ; Endtag ++ ;}  //  Print          For ( Int  TT: Least) system. Out. Print (TT + "" ); System. Out. println ();  For ( Int  TT: colorarray) system. Out. Print (TT + "" );  Return  Colorarray ;}} 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.