Twitter OA prepare: K-complementary pair

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的更多相关文章
- 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: 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: 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 ...
- 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 ...
- 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 ...
- 2Sigma OA prepare: Longest Chain
DP use HashMap: 根据string的长度sort,然后维护每个string的longest chain,default为1,如果删除某个char生成的string能提供更长的chain, ...
随机推荐
- google v8引擎常见问题
最近在项目中使用v8来进行扩展,下面简单说一下使用v8过程中遇到的一些问题. v8的多线程调用 最初调研v8的测试代码是单线程的,后来一个项目在多线程中使用,出现了一些问题,后来看到参考3中的才恍 ...
- Elasticsearch学习之相关度评分TF&IDF
relevance score算法,简单来说,就是计算出,一个索引中的文本,与搜索文本,他们之间的关联匹配程度 Elasticsearch使用的是 term frequency/inverse doc ...
- mysql架构图
整体架构图 访问控制图
- 从浏览器输入URL到页面渲染的过程
零.背景 一个web安全工程师在学习web安全和web渗透时候,非常有必要了解整个WEB工作过程. 一.输入URL 这里是最基本的知识:URL是URI的一种实际应用,URI统一资源表示符,URL统一资 ...
- 剑指offer题目记录
1.如下为类型CMyString的声明,请为该类型添加赋值运算符函数. class CMyString { public: CMyString(char* pData = NULL); CMyStri ...
- Root Motion的脚本处理
一.Apply RootMotion的作用 Apply RootMotion:控制角色在场景中动画的运动.角色动画驱动角色运动,类似于在游戏中直接使用动画,提高了动画的使用效率. Root Motio ...
- Redis学习资料整理
Redis学习资料: (1)Redis设计与实现 (2)十五分钟介绍 Redis数据结构 (3)redis安装 (4)redis指令手册中文版 Hiredis学习资料: (1)hiredis安装及测试 ...
- 《机器学习实战》2.2.2分析数据:使用matplotlib创建散点图
#输出散点图 def f(): datingDataMat,datingLabels = file2matrix("datingTestSet3.txt") fig = plt.f ...
- 51nod 1183 - 编辑距离 - [简单DP][编辑距离问题][Levenshtein距离问题]
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1183 编辑距离,又称Levenshtein距离(也叫做Edi ...
- Redis used_cpu_sys used_cpu_user meaning (redis info中cpu信息的含义)
Redis 中 used_cpu_sys 和 used_cpu_user含义. 在Redis的info命令输出结果中有如下四个指标,redis官网给出了下面一段解释,但是还是不明白什么意思. used ...