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这道题的基础上还要做一定修改,记录区间边缘移动情况

 public int flipping(int[] A) {
int local = 0;
int global = 0;
int localL = 0;
int localR = 0;
int globalL = 0;
int globalR = 0;
int OnesFlip = 0;
int OnesUnFlip = 0; //those # of ones outside the chosen range
for (int i=0; i<A.length; i++) {
int diff = 0;
if (A[i] == 0) diff = 1;
else diff = -1; if (local + diff >= diff) {
local = local + diff;
localR = i;
}
else {
local = diff;
localL = i;
localR = i;
} if (global < local) {
global = local;
globalL = localL;
globalR = localR;
}
}
for (int i=0; i<globalL; i++) {
if (A[i] == 1)
OnesUnflip ++;
}
    for (int i=globalL; i<=globalR; i++) {
if (A[i] == 1)
       OnesFlip ++;
}
for (int i=globalR+1; i<A.length; i++) {
if (A[i] == 1)
OnesUnflip ++;
}
return (global+OnesFlip) + OnesUnflip;
}

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

  1. Twitter OA prepare: Two Operations

    准备T家OA,网上看的面经 最直接的方法,从target降到1,如果是奇数就减一,偶数就除2 public static void main(String[] args) { int a = shor ...

  2. Twitter OA prepare: even sum pairs

    思路:无非就是扫描一遍记录奇数和偶数各自的个数,比如为M和N,然后就是奇数里面选两个.偶数里面选两个,答案就是M(M-1)/2 + N(N-1)/2

  3. Twitter OA prepare: K-complementary pair

    2sum的夹逼算法,需要sort一下.本身不难,但是tricky的地方在于允许同一个数组元素自己跟自己组成一个pair,比如上例中的[5, 5].而且数组本身就允许值相等的元素存在,在计算pair时, ...

  4. Twitter OA prepare: Anagram is A Palindrome

    Algorithm: Count the number of occurrence of each character. Only one character with odd occurrence ...

  5. Twitter OA prepare: Visit element of the array

    分析:就是建立一个boolean array来记录array里面每个元素的访问情况,遇到访问过的元素就停止visiting,返回未访问的结点个数 public int visiting(int[] A ...

  6. 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 ...

  7. 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 ...

  8. 2Sigma OA prepare: Longest Chain

    DP use HashMap: 根据string的长度sort,然后维护每个string的longest chain,default为1,如果删除某个char生成的string能提供更长的chain, ...

  9. 2Sigma OA prepare: Friends Circle

    DFS & BFS: 关键在于构造graph package twoSigma; import java.util.ArrayList; import java.util.HashSet; i ...

随机推荐

  1. 4G厂商版《出师表》

    转载:http://mp.weixin.qq.com/s?__biz=MzAwNDAyODM0NA==&mid=408013599&idx=1&sn=70cd33d037604 ...

  2. Nhibernate学习

    Contains 缓存中是否存在SessionFactoryBizCom.GetInstance().GetCurrentSession().Contains(t1) Evict 临时状态(Trans ...

  3. visual studio 2013设置背景图片

    今天听了公司的一个经验分享会,发现VS竟然可以设置背景图片!还是个萌妹子!!被萌了一脸鼻血!!! 设置方法很简单:安装扩展ClaudiaIDE 1.在这里下载扩展,https://visualstud ...

  4. Windows Server 2008 R2之一活动目录服务部署

    测试环境: 服务器:计算机名Win2008R2CNDC,已安装Windows Server 2008 R2.IPV4:192.168.1.13,255.255.255.0,网关地址192.168.1. ...

  5. vue--引入富文本编辑器

    https://blog.csdn.net/div_ma/article/details/79536634 // 使用 https://blog.csdn.net/div_ma/article/det ...

  6. vue之修饰符

    修饰符 .lazy 在默认情况下,v-model 在每次 input 事件触发后将输入框的值与数据进行同步 .你可以添加 lazy 修饰符,从而转变为使用 change 事件进行同步: <!-- ...

  7. OSS命令行工具ossutil

    ossutil工具旨在为您提供一个以命令行方式管理OSS数据的途径.当前版本未提供完整的Bucket和Multipart管理功能,相关功能会在后续版本中开发.如果您需要使用上述功能,建议先使用ossc ...

  8. Docker 学习应用篇之二: Docker的介绍和安装

    之前说过Docker的好处,Docker可以集装箱化的部署应用程序.那么Docker是通过什么实现的呢.要理解Docker内部构建,需要先理解Docker的四种部件 1)images:镜像,docke ...

  9. SSH免密码登录配置方法详解

    1.测试主机配置信息: 192.168.100.236 db06.chavin.king db06 192.168.100.237 db07.chavin.king db07 2.创建测试用户: gr ...

  10. Jena 操作 RDF 文件

    1. RDF 入门 RDF(Resource Description Framework)是由W3C规定的,描述资源(resource)的数据模型(data model),: RDF 使用Web标识符 ...