一亩三分地论坛

 找回密码
 获取更多干货,去instant注册!

扫码关注一亩三分地公众号
查看: 854|回复: 2
收起左侧

Twitter OA

[复制链接] |试试Instant~ |关注本帖
rwei 发表于 2014-12-23 06:27:32 | 显示全部楼层 |阅读模式

2014(10-12月) 码农类 本科 全职@Twitter - 猎头 - 在线笔试 |Pass

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看,没有帐号?获取更多干货,去instant注册!

x

Twitter OA:

(1)

Twosum from Leetcode

第一題上 Leetcode 看

https://oj.leetcode.com/problems/two-sum/
. Waral 鍗氬鏈夋洿澶氭枃绔,
(2)
鏉ユ簮涓浜.涓夊垎鍦拌鍧.
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.
.鏈枃鍘熷垱鑷1point3acres璁哄潧
What is the maximum number of '1'-bits (indicated by S) which you can obtain in the final bit-string?

'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
. 1point3acres.com/bbs
Constraints:
1 <= N <= 100000 . 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴
d can only be 0 or 1f . 1point 3acres 璁哄潧
0 <= L <= R < n

Sample Input:
8
1 0 0 1 0 0 1 0

Sample Output:
6

Explanation: .鏈枃鍘熷垱鑷1point3acres璁哄潧

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. 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴


這題可以這麼做:

public static void flipBits(int[] a) {
. 鍥磋鎴戜滑@1point 3 acres
        int maxDiff = 0;
        int flipStartIndex = 0;
        int flipEndIndex = 0;. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
        int onesToFlip = 0;
        int totalNumberOfOnes = 0;

        int currentDiff = 0;
        int currentStart = 0;
        int currentOnesToFlip = 0;

        for (int i = 0; i < a.length; i++) {
            if (a == 0) {. from: 1point3acres.com/bbs
                        currentDiff -= 1;. 1point 3acres 璁哄潧
            } else {. 1point3acres.com/bbs
                        currentDiff += 1;
                        currentOnesToFlip++;. 1point 3acres 璁哄潧
                        totalNumberOfOnes++;
            }
            if (currentDiff < maxDiff) {
                        maxDiff = currentDiff;. 鍥磋鎴戜滑@1point 3 acres
                        flipStartIndex = currentStart;
                        flipEndIndex = i;
                        onesToFlip = currentOnesToFlip;
            } else if (currentDiff > 0) {. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
                        currentDiff = 0;
                        currentStart = i + 1;
                        currentOnesToFlip = 0;
            }. From 1point 3acres bbs
        }
        System.out.println(flipEndIndex - flipStartIndex + 1 - onesToFlip +   totalNumberOfOnes - onesToFlip);
}

Space O(1), Time O(n).鏈枃鍘熷垱鑷1point3acres璁哄潧



yannan 发表于 2015-2-7 11:37:31 | 显示全部楼层
我今天OA第二题和你一样!
回复 支持 反对

使用道具 举报

 楼主| rwei 发表于 2015-2-7 11:39:49 来自手机 | 显示全部楼层
yannan 发表于 2015-2-7 11:37
我今天OA第二题和你一样!

估計最近的都是吧…命中了求加分
回复 支持 反对

使用道具 举报

本版积分规则

请点这里访问我们的新网站:一亩三分地Instant.

Instant搜索更强大,不扣积分,内容组织的更好更整洁!目前仍在beta版本,努力完善中!反馈请点这里

关闭

一亩三分地推荐上一条 /5 下一条

手机版|小黑屋|一亩三分地论坛声明 ( 沪ICP备11015994号 )

custom counter

GMT+8, 2016-12-7 18:27

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

快速回复 返回顶部 返回列表