1. You are given a binary array with N elements: d[0], d[1], ... d[N - 1].
  2. 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.
  3.  
  4. 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
  5.  
  6. 'Flipping' a bit means, that a 0 is transformed to a 1 and a 1 is transformed to a 0 (0->1,1->0).
  7. Input Format
  8. An integer N
  9. Next line contains the N bits, separated by spaces: d[0] d[1] ... d[N - 1]
  10.  
  11. Output:
  12. S
  13.  
  14. Constraints:
  15. 1 <= N <= 100000
  16. d can only be 0 or 1f -google 1point3acres
  17. 0 <= L <= R < n
  18. . 1point3acres.com/bbs
  19. Sample Input:
  20. 8
  21. 1 0 0 1 0 0 1 0 . 1point3acres.com/bbs
  22.  
  23. Sample Output:
  24. 6
  25.  
  26. Explanation:
  27.  
  28. We can get a maximum of 6 ones in the given binary array by performing either of the following operations:
  29. 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;
    int OnesFlip = 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. }
        for (int i=globalL; i<=globalR; i++) {
    if (A[i] == 1)
           OnesFlip ++;
    }
  34. for (int i=globalR+1; i<A.length; i++) {
  35. if (A[i] == 1)
  36. OnesUnflip ++;
  37. }
  38. return (global+OnesFlip) + OnesUnflip;
  39. }

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. PHP内置安全函数一览

    内置安全函数 filter_var函数 根据参数中的过滤类型进行过滤,如过滤Email类型的,则符合的字符串返回字符串,不符合的返回False. urldecode函数 写这个函数是特别为了提醒注意, ...

  2. ASP.NET MVC View使用Conditional compilation symbols

    由于View(.cshtml)的运行时编译关系,在项目级别中定义的symbols是无法被直接使用的.需要在Web.config中添加compilerOptions(在View目录下的Web.confi ...

  3. javaagent

    -javaagent:<jarpath>[=<options>]load Java programming language agent, see java.lang.inst ...

  4. python requests模块中返回时间elapsed解析

    一.问题: Python 中requests库在发送http请求时相当方便好用,但在使用时一直受一个问题困扰,怎么才能查看请求时长呢? 自己写时间函数再相减?NO,这个方法肯定不行. 二.解决: 好吧 ...

  5. .NET中的三种Timer的区别和用法(收集)

    最近正好做一个WEB中定期执行的程序,而.NET中有3个不同的定时器.所以正好研究研究.这3个定时器分别是: 1.实现按用户定义的时间间隔引发事件的计时器.此计时器最宜用于 Windows 窗体应用程 ...

  6. mac操作

    资料搜集: mac终端 常用命令操作 mac osx常用快捷键一览 mac chrome快捷键

  7. Windows Server 2008 R2之管理Sysvol文件夹

    以下是Sysvol文件夹示例图 Domain文件夹:是策略的实体,是策略和脚本存放地. Staging Areas:交换区,它用来存放多台DC之间交换(复制)的信息.DC上的相关信息(GPO)首先将要 ...

  8. geotrellis使用(三十二)大量GeoTiff文件实时发布TMS服务

    前言 在上一篇文章中我讲了如何直接将Geotiff文件发布为TMS服务,在其中只讲了单幅Geotiff的操作,其实单幅这种量级的数据对Geotrellis来说就是杀鸡焉用牛刀,Geotrellis针对 ...

  9. nginx 开机自动启动

    接下来聊一聊nginx的开机自启吧 看了看都是用脚本启动的,我也就不扯啥犊子了,都是前人经验 我的操作系统是centos 7 nginx版本是1.10.3 首先看一下自己的nginx配置 我的是 ./ ...

  10. zabbix中文乱码的问题

    在使用zabbix时,有时候会出现中文乱码的问题,如下: 因为zabbix自身对中文简体的支持不完善,需要我们手动的去上传新的字体进行替换: 1.在windows获取字体库文件 在Windows上的字 ...