2sum的夹逼算法,需要sort一下。本身不难,但是tricky的地方在于允许同一个数组元素自己跟自己组成一个pair,比如上例中的[5, 5]。而且数组本身就允许值相等的元素存在,在计算pair时,算成不同的pair,比如数组是[3,3],K=6,这时的pair有[0, 0], [0, 1], [1, 0], [1, 1]4个。

这个case让这道本来不难的题一下子麻烦了许多。我的对应处理方法是:用HashMap记录每个元素出现次数,用一个变量res记录可行pair数。 像夹逼方法那样,一左一右两个pointer l、r 分别往中间走,如果左右元素和加起来等于K:

1. 如果 l == r, res = res + 1;

2. else, 如果A[l] == A[r], res = res + Math.pow(map.get(A[l]), 2); 加上A[l]出现次数的平方,比如[3, 3]这个例子,加上2^2 ==4, 再如[3, 3, 3], 加9

3. else, 即A[l] != A[r], res = res + 2 * map.get(A[l]) * map.get(A[r]); 比如[1, 1, 5], 加上4;[1, 1, 5, 5],加上8;[-2, 8], 加上2

然后就是之后左右pointer该怎么跳,我的处理方法是,跳出现次数那么多次,这样就不再重复处理这些出现过的数字

 public int KComplementary(int[] A, int K) {
if (A==null || A.length==0) return 0;
int res = 0;
int l = 0;
int r = A.length - 1;
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i=0; i<A.length; i++) {
if (map.containsKey(A[i])) {
map.put(A[i], map.get(A[i])+1);
}
else {
map.put(A[i], 1);
}
}
while (l <= r) {
if (A[l] + A[r] == K) {
if (l == r) res += 1;
else if (A[l] == A[r]) {
res += Math.pow(map.get(A[l]), 2);
}
else {
res += 2 * map.get(A[l]) * map.get(A[r]);
}
l = l + map.get(A[l]);
r = r - map.get(A[r]);
}
else if (A[l] + A[r] < K) {
l = l + map.get(A[l]);
}
else {
r = r - map.get(A[r]);
}
}
return res;
}

Twitter OA prepare: K-complementary pair的更多相关文章

  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: Anagram is A Palindrome

    Algorithm: Count the number of occurrence of each character. Only one character with odd occurrence ...

  4. Twitter OA prepare: Visit element of the array

    分析:就是建立一个boolean array来记录array里面每个元素的访问情况,遇到访问过的元素就停止visiting,返回未访问的结点个数 public int visiting(int[] A ...

  5. 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 ...

  6. 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 ...

  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. FB面经 Prepare: K closest point to the origin

    Give n points on 2-D plane, find the K closest points to origin Based on bucket sort: package fbPrac ...

  9. 2Sigma OA prepare: Longest Chain

    DP use HashMap: 根据string的长度sort,然后维护每个string的longest chain,default为1,如果删除某个char生成的string能提供更长的chain, ...

随机推荐

  1. android include标签的使用,在RelativeLayout中使用include标签需注意!!!!!

    转:http://4265337.blog.163.com/blog/static/195375820127935731114/ include和merge标记的作用主要是为了解决layout的重用问 ...

  2. call()、apply()和bind()的异同

    相同点: 改变this的指向: var a = { name:"丸子", fn:function(){ console.log(this.name); } } var b = a. ...

  3. Linux 常用查找文件或者文件内容

    举例树形图 .|-- test_dir| `-- dir_test_doc.text|-- test_dir2| |-- dir2_test_doc.txt| `-- dir2_test_doc2.t ...

  4. 未能加载文件或程序集“XX.XXX.Web”或它的某一个依赖项。试图加载格式不正确的程序

    IIS应用程序池->右键高级设置->启用32位应用程序  设置为true

  5. 在 arc里面打印 引用计数的方法

    查阅资料:   You can use CFGetRetainCount with Objective-C objects, even under ARC: NSLog(@"Retain c ...

  6. yii---load怎么使用

    在用YII进行二次开发的时候,看到登录方法有一个load的方法: public function actionLogin() { if (Yii::$app->request->isPos ...

  7. 公司HBase基准性能测试之结果篇

    上一篇文章<公司HBase基准性能测试之准备篇>中详细介绍了本次性能测试的基本准备情况,包括测试集群架构.单台机器软硬件配置.测试工具以及测试方法等,在此基础上本篇文章主要介绍HBase在 ...

  8. linux下面安装coreseek与mmseg

    1tar xzvf coreseek-3.2.14.tar.gz2cd mmseg-3.2.14/./configure --prefix=/usr/local/mmseg3 checking for ...

  9. 结对编程2—Fault&Error&Failure

    学习进度表 点滴成就 学习时间 新编写代码行数 博客量(篇) 学到知识点 第一周 8 0 0 了解软件工程 第二周 10 0 1 博文一篇 第三周 15 0 2 选择项目.调查问卷 第四周 20 80 ...

  10. Git 使用篇二:小组协作开发

    上一片搭建了git远程服务器,那么小组成员在使用git开发的时候都有什么要注意的. 第一步: 首先每个小组成员,在自己本地建立一个目录,作为工作空间,再去git clone 这个远程仓库: git c ...