Twitter OA prepare:flipping a bit

Source: Internet
Author: User

You is given a binary array with N elements:d[0], d[1], ... d[n-1]. You can perform at most one move on the array:choose any and integers [L, R], and flip all the elements between (and incl uding) The L-th and r-th bits. L and R represent the left-most and right-The most index of the bits marking the boundaries of the segment which you has decided to flip. What is the maximum number of' 1 '-bits (indicated by S) which your can obtain in theFinalBit-string?. More info on 1point3acres.com' Flipping ' a bit means, that a 0 was transformed to a 1 and a 1 are transformed to a 0 (0->1,1->0). Input Format an integer n Next line contains the N bits, separated by spaces:d[0] d[1] ... d[n-1] output:s Constraints:1 <= N <= 100000D can only is0 or 1f-Google 1point3acres0 <= L <= R <N. 1point3acres.com/bbssample Input:8 1 0 0 1 0 0 1 0. 1point3acres.com/bbssample Output:6Explanation:we can get a maximum of6ones in the given binary array by performing either of the following Operations:flip [1, 5] ==> 1 1 1 0 1 1 1 0

Analysis: This problem is nothing more than in an array, find an interval, the number of the interval 0 and 1 of the number of the largest difference. If we think of this as a stock, 0 for +1,1 represents-1, then this problem is converted to the best time to Buy and Sell the stock, looking for 0 and 1 of the number of difference between the maximum is to find max profit.

Because of the need to find this interval, so on the basis of the stock of the problem must be modified to record the interval edge movement situation

1  Public intFlipping (int[] A) {2    intLocal = 0;3    intGlobal = 0;4    intLocalL = 0;5    intLOCALR = 0;6    intGloball = 0;7    intGLOBALR = 0;8    intOnesunflip = 0;//those # of ones outside the chosen range9     for(inti=0; i<a.length; i++) {Ten         intdiff = 0; One         if(A[i] = = 0) diff = 1; A         Elsediff =-1; -  -         if(Local + diff >=diff) { theLocal = local +diff; -LOCALR =i; -         } -         Else { +Local =diff; -LocalL =i; +LOCALR =i; A         } at  -         if(Global <local) { -Global =Local; -Globall =LocalL; -GLOBALR =localr; -         } in    } -     for(inti=0; i<globall; i++) { to         if(A[i] = = 1)  +Onesunflip + +; -    } the     for(inti=globalr+1; i<a.length; i++) { *         if(A[i] = = 1)  $Onesunflip + +;Panax Notoginseng    } -    returnGlobal +Onesunflip; the}

Twitter OA prepare:flipping a bit

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.