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这道题的基础上还要做一定修改,记录区间边缘移动情况
- 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的更多相关文章
- Twitter OA prepare: Two Operations
准备T家OA,网上看的面经 最直接的方法,从target降到1,如果是奇数就减一,偶数就除2 public static void main(String[] args) { int a = shor ...
- Twitter OA prepare: even sum pairs
思路:无非就是扫描一遍记录奇数和偶数各自的个数,比如为M和N,然后就是奇数里面选两个.偶数里面选两个,答案就是M(M-1)/2 + N(N-1)/2
- Twitter OA prepare: K-complementary pair
2sum的夹逼算法,需要sort一下.本身不难,但是tricky的地方在于允许同一个数组元素自己跟自己组成一个pair,比如上例中的[5, 5].而且数组本身就允许值相等的元素存在,在计算pair时, ...
- Twitter OA prepare: Anagram is A Palindrome
Algorithm: Count the number of occurrence of each character. Only one character with odd occurrence ...
- Twitter OA prepare: Visit element of the array
分析:就是建立一个boolean array来记录array里面每个元素的访问情况,遇到访问过的元素就停止visiting,返回未访问的结点个数 public int visiting(int[] A ...
- 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 ...
- 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 ...
- 2Sigma OA prepare: Longest Chain
DP use HashMap: 根据string的长度sort,然后维护每个string的longest chain,default为1,如果删除某个char生成的string能提供更长的chain, ...
- 2Sigma OA prepare: Friends Circle
DFS & BFS: 关键在于构造graph package twoSigma; import java.util.ArrayList; import java.util.HashSet; i ...
随机推荐
- PHP内置安全函数一览
内置安全函数 filter_var函数 根据参数中的过滤类型进行过滤,如过滤Email类型的,则符合的字符串返回字符串,不符合的返回False. urldecode函数 写这个函数是特别为了提醒注意, ...
- ASP.NET MVC View使用Conditional compilation symbols
由于View(.cshtml)的运行时编译关系,在项目级别中定义的symbols是无法被直接使用的.需要在Web.config中添加compilerOptions(在View目录下的Web.confi ...
- javaagent
-javaagent:<jarpath>[=<options>]load Java programming language agent, see java.lang.inst ...
- python requests模块中返回时间elapsed解析
一.问题: Python 中requests库在发送http请求时相当方便好用,但在使用时一直受一个问题困扰,怎么才能查看请求时长呢? 自己写时间函数再相减?NO,这个方法肯定不行. 二.解决: 好吧 ...
- .NET中的三种Timer的区别和用法(收集)
最近正好做一个WEB中定期执行的程序,而.NET中有3个不同的定时器.所以正好研究研究.这3个定时器分别是: 1.实现按用户定义的时间间隔引发事件的计时器.此计时器最宜用于 Windows 窗体应用程 ...
- mac操作
资料搜集: mac终端 常用命令操作 mac osx常用快捷键一览 mac chrome快捷键
- Windows Server 2008 R2之管理Sysvol文件夹
以下是Sysvol文件夹示例图 Domain文件夹:是策略的实体,是策略和脚本存放地. Staging Areas:交换区,它用来存放多台DC之间交换(复制)的信息.DC上的相关信息(GPO)首先将要 ...
- geotrellis使用(三十二)大量GeoTiff文件实时发布TMS服务
前言 在上一篇文章中我讲了如何直接将Geotiff文件发布为TMS服务,在其中只讲了单幅Geotiff的操作,其实单幅这种量级的数据对Geotrellis来说就是杀鸡焉用牛刀,Geotrellis针对 ...
- nginx 开机自动启动
接下来聊一聊nginx的开机自启吧 看了看都是用脚本启动的,我也就不扯啥犊子了,都是前人经验 我的操作系统是centos 7 nginx版本是1.10.3 首先看一下自己的nginx配置 我的是 ./ ...
- zabbix中文乱码的问题
在使用zabbix时,有时候会出现中文乱码的问题,如下: 因为zabbix自身对中文简体的支持不完善,需要我们手动的去上传新的字体进行替换: 1.在windows获取字体库文件 在Windows上的字 ...