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 ...
随机推荐
- Linux下应急工具
Linux下的应急工具 在Linux下,应急的查看点无非那个几个,一是看表现(宕机.高CPU.高内存.高IO.高网络通信),二看连接.三看进程.四看日志.五看文件(Linux一切皆文件),再者结合起来 ...
- MVC验证
前言 MVC自己的验证机制,通过一个案例记录学习的成果. 首先,model代码如下: public class Students { [Display(Name = "I ...
- 【CF884F】Anti-Palindromize 费用流
[CF884F]Anti-Palindromize 题意:定义一个串是反回文的,当且仅当对于1<=i<=len,$a_i!=a_{len-i+1}$. 现在给出一个长度为n的串S(n是偶数 ...
- 应用程序添加角标和tabBar添加角标,以及后台运行时显示
1.设置角标的代码: // 从后台取出来的数据可能是int型的不能直接给badgeValue(string类型的),需要通过description转化 NSString *count = [re ...
- POJ 2653 - Pick-up sticks - [枚举+判断线段相交]
题目链接:http://poj.org/problem?id=2653 Time Limit: 3000MS Memory Limit: 65536K Description Stan has n s ...
- HDU 2444 - The Accomodation of Students - [二分图判断][匈牙利算法模板]
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Time Limit: 5000/1000 MS (Java/Others) Mem ...
- Oracle to_date()函数的用法介绍
to_date()是Oracle数据库函数的代表函数之一,下文对Oracle to_date()函数的几种用法作了详细的介绍说明,需要的朋友可以参考下 在Oracle数据库中,Oracle t ...
- php:// — 访问各个输入/输出流(I/O streams)
PHP: php:// - Manual http://www.php.net/manual/zh/wrappers.php.php php:// php:// — 访问各个输入/输出流(I/O st ...
- 2018/04/04 每日一个Linux命令 之 ps
ps 用于查看系统内的进程状态. 这个命令比较重要,也比较长,会通过实践出常用的命令 -- 当我们敲下一个 ps 之后会发生什么? ubuntu@hong:~/nginx/sites-enabled$ ...
- 虚拟机linux centoros系统安装
(一) 系统下载地址:https://www.centos.org/download/ (二) 下载安装:vmware.并安装. (三) 虚拟机的安装: 1.创建新的虚拟机 2.选择自定义,下一步 3 ...