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. javascript取querystring,存储为hash

    function getUrlVars() { var vars = [], hash; var hashes = window.location.href.slice(window.location ...

  2. 编译安装的gitlab8.x如何修改时区设置

    编译安装的gitlab 8.x版本默认的时区是UTC,在页面上显示的时间默认是零时区的区时,安装完成之后,如果页面上显示的时间比北京时间少了8个小时,则需要修改一下时区 把gitlab.yml文件中的 ...

  3. DragonBones龙骨发布后在Egret中的位置

    DragonBones发布后的动画,加载到Egret中场景中,原点的位置在哪呢? DragonBones中的图片位置 导出 加载到Egret中.可见DragonBones中的图片位置原点左下方(0,0 ...

  4. Unity3D笔记十七 Unity3D生命周期

    一个游戏组件的脚本有一个生命周期——一开始实例化,直到结束实例被销毁.在这期间,他们有时候处于激活状态,有时候处于非激活状态:对于活动,对用户有时候可见,有时候不可见 本文主要讨论常见脚本的的生命周期 ...

  5. Bitbucket - 用git 用法

    核心流程: 从远端中心repo那里Git clone 到本地,再在本地开发(add, commit), 通常会利用branch管理,如果觉得code 没问题了,就push到远端的中心repo上.这里中 ...

  6. python-django开发学习笔记四

    先插入一条广告,博主新开了一家淘宝店,经营自己纯手工做的发饰,新店开业,只为信誉!需要的亲们可以光顾一下!谢谢大家的支持!店名: 小鱼尼莫手工饰品店经营: 发饰.头花.发夹.耳环等(手工制作)网店: ...

  7. OOA/D 01

    建筑师一般不会为一栋100层的楼添加一个新的地下室,因为成本太高无疑会失败,但软件系统里提出类似改动需求时,他们通常都不会多想一下,相反他们会说:这只是一个简单的编程问题 可总会有一些看似极难完成.但 ...

  8. 170817、Nginx详细配置

    Nginx能做什么 nginx主要是做转发,当然也可以做静态资源文件缓存,做转发的时候,比如你有几个url,可以统一通过走nginx,然后通过nginx转发到不同的url上 1.反向代理 反向代理应该 ...

  9. OpenCV学习笔记之课后习题练习3-5

    OpenCV学习笔记之课后习题练习2-5 练习使用感兴趣区域(ROI).创建一个210*210的单通道图像并将其归0.在图像中使用ROI和cvSet()建立一个增长如金字塔状的数组. 参考博文:www ...

  10. the internal array pointer

    The foreach construct provides an easy way to iterate over arrays. foreach works only on arrays and ...