Twitter OA prepare: Flipping a bit

You are 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 two integers [L, R], and flip all the elements between (and including) the L-th and R-th bits. L and R represent the left-most and right-most index of the bits marking the boundaries of the segment which you have decided to flip.

What is the maximum number of ‘1‘-bits (indicated by S) which you can obtain in the final bit-string? . more info on 1point3acres.com

‘Flipping‘ a bit means, that a 0 is transformed to a 1 and a 1 is 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 <= 100000
d can only be 0 or 1f -google 1point3acres
0 <= L <= R < n
. 1point3acres.com/bbs
Sample Input:
8
1 0 0 1 0 0 1 0 . 1point3acres.com/bbs

Sample Output:
6 

Explanation: 

We can get a maximum of 6 ones in the given binary array by performing either of the following operations:
Flip [1, 5] ==> 1 1 1 0 1 1 1 0

分析:这道题无非就是在一个数组内,找一个区间,该区间  0的个数  与  1的个数  差值最大。如果我们把这个想成股票的话,0代表+1,1代表-1,那么这道题就转化成了Best Time to Buy and Sell Stock, 找0和1的个数差值最大就变成了找max profit。

因为需要找到这个区间,所以在Stock这道题的基础上还要做一定修改,记录区间边缘移动情况

 1 public int flipping(int[] A) {
 2    int local = 0;
 3    int global = 0;
 4    int localL = 0;
 5    int localR = 0;
 6    int globalL = 0;
 7    int globalR = 0;
 8    int OnesUnFlip = 0;   //those # of ones outside the chosen range
 9    for (int i=0; i<A.length; i++) {
10         int diff = 0;
11         if (A[i] == 0) diff = 1;
12         else diff = -1;
13
14         if (local + diff >= diff) {
15             local = local + diff;
16             localR = i;
17         }
18         else {
19             local = diff;
20             localL = i;
21             localR = i;
22         }
23
24         if (global < local) {
25             global = local;
26             globalL = localL;
27             globalR = localR;
28         }
29    }
30    for (int i=0; i<globalL; i++) {
31         if (A[i] == 1)
32             OnesUnflip ++;
33    }
34    for (int i=globalR+1; i<A.length; i++) {
35         if (A[i] == 1)
36             OnesUnflip ++;
37    }
38    return global + OnesUnflip;
39 }
时间: 2024-10-12 03:55:25

Twitter OA prepare: Flipping a bit的相关文章

Twitter OA prepare: K-complementary pair

A non-empty zero-indexed array A consisting of N integers is given. A pair of integers (P, Q) is called K-complementary in array A if 0 ≤ P, Q < N and A[P] + A[Q] = K. For example, consider array A such that: A[0] = 1 A[1] = 8 A[2]= -3 A[3] = 0 A[4]

Twitter OA prepare: Equilibrium index of an array

Equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes. For example, in an arrya A: A[0] = -7, A[1] = 1, A[2] = 5, A[3] = 2, A[4] = -4, A[5] = 3, A[6]=0 3 is an equil

Twitter OA prepare: even sum pairs

Write a function: class Solution { public int solution(int[] A); } that, given an array A consisting of N integers, returns the number of pairs (P, Q) such that 0 ≤ P < Q < N and (A[P] + A[Q]) is even. The function should return −1 if the number of

Twitter OA prepare: Anagram is A Palindrome

A string is a palindrome if it has exactly the same sequence of characters when traversed left-to-right as right-to-left. For example, the following strings are palindromes: "kayak" "codilitytilidoc" "neveroddoreven" A string

Twitter OA prepare: Visit element of the array

A zero-indexed array A consisting of N integers is viven. We visit the indexs of the array in the following way. In the first step we visit the index 0; in every subsequent step we move from the visited index K to the index: M = K + A[K]; provided M

Twitter OA prepare: Rational Sum

In mathematics, a rational number is any number that can be expressed in the form of a fraction p/q , where p & q are two integers, and the denominator q is not equal to zero. Hence, all integers are rational numbers where denominator, in the most re

2Sigma OA prepare: Longest Chain

DP use HashMap: 根据string的长度sort,然后维护每个string的longest chain,default为1,如果删除某个char生成的string能提供更长的chain,则更新 1 package twoSigma; 2 3 import java.util.Arrays; 4 import java.util.Comparator; 5 import java.util.HashMap; 6 import java.lang.StringBuilder; 7 8

2Sigma OA prepare: Friends Circle

DFS & BFS: 关键在于构造graph 1 package twoSigma; 2 3 import java.util.ArrayList; 4 import java.util.HashSet; 5 import java.util.LinkedList; 6 import java.util.Queue; 7 8 public class FriendCircle1 { 9 ArrayList<ArrayList<Integer>> graph; 10 publ

【转】Twitter Storm如何保证消息不丢失

Twitter Storm如何保证消息不丢失 发表于 2011 年 09 月 30 日 由 xumingming 作者: xumingming | 可以转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明网址: http://xumingming.sinaapp.com/127/twitter-storm如何保证消息不丢失/ 本文翻译自: https://github.com/nathanmarz/storm/wiki/Guaranteeing-message-processing s