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. Post Office Protocol --- pop协议

    https://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol

  2. Kafka usecase

    h1, h2, h3, h4, h5, h6, p, blockquote { margin: 5px; padding: 5; } body { font-family: "Helveti ...

  3. Fiddler实现手机的抓包(转载园友小坦克)

    Fiddler不但能截获各种浏览器发出的HTTP请求, 也可以截获各种智能手机发出的HTTP/HTTPS请求. Fiddler能捕获IOS设备发出的请求,比如IPhone, IPad, MacBook ...

  4. STS没有找到Dynamic Web Project

    解决:安装JavaEE插件 help-> install new software-> 选择sts对应的eclipse版本站点,如eclipse版本4.09选择2018-09.4.10选择 ...

  5. GlusterFS实战

    预装glusterfs软件包 yum -y install centos-release-gluster37.noarch yum --enablerepo=centos-gluster*-test ...

  6. POJ-1088 滑雪 (记忆化搜索,dp)

    滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 86318 Accepted: 32289 Description Mich ...

  7. OpenCV学习笔记之CXCORE篇

    转自blog.csdn.net/bbzz2/article/details/50764209

  8. 51nod 1009 - 数字1的数量 - [数位DP][模板的应用以及解释]

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1009 基准时间限制:1 秒 空间限制:131072 KB 给 ...

  9. 从经典面试题看java中类的加载机制

    1.概述 类加载是Java程序运行的第一步,研究类的加载有助于了解JVM执行过程,并指导开发者采取更有效的措施配合程序执行,对理解java虚拟机的连接模型和java语言的动态性都有很大帮助. 由于Ja ...

  10. HDFS Users Guide

    Purpose This document is a starting point for users working with Hadoop Distributed File System (HDF ...