Codility K complementary pairs
2016-03-20 16:04
471 查看
codelity kcomplementary pairs
这里要注意找到一对(A[i],A[j])之后, 除非i==j,不然要算两次。例如这里(4,8)和(8,4)。
这里提供两种方法。一种用dict记录key = A element value = the distinct number of A element. 然后循环这个dict就行。第二种就是先sort,然后首尾two pointers,不断向中间靠拢。这里值得注意的就是说重复值,A[i]之后如果有一串重复值,A[j]之后也有一串重复值,那么就要用while判断出各自重复了多少次,然后次数相乘并且乘以2,因为一个pair要算两次,除非i == j.
A non-empty zero-indexed array A consisting of N integers is given. A pair of integers (P, Q) is called K-complementary in array A if 0 ≤ P, Q < N and A[P] + A[Q] = K. For example, consider array A such that: A[0] = 1 A[1] = 8 A[2]= -3 A[3] = 0 A[4] = 1 A[5]= 3 A[6] = -2 A[7] = 4 A[8]= 5 The following pairs are 6-complementary in array A: (0,8), (1,6), (4,8), (5,5), (6,1), (8,0), (8,4).#这里要注意找到一对(A[i],A[j])之后, 除非i==j,不然要算两次。例如这里(4,8)和(8,4) For instance, the pair (4,8) is 6-complementary because A[4] + A[8] = 1 + 5 = 6. Write a function: class Solution { public int solution(int K, int[] A); } that, given an integer K and a non-empty zero-indexed array A consisting of N integers, .1point3acres缃� returns the number of K-complementary pairs in array A. For example, given K = 6 and array A such that: A[0] = 1 A[1] = 8 A[2]= -3 A[3] = 0 A[4] = 1 A[5]= 3 A[6] = -2 A[7] = 4 A[8]= 5 the function should return 7, as explained above. Assume that: N is an integer within the range [1..50,000];-google 1point3acres K is an integer within the range [−2,147,483,648..2,147,483,647]; each element of array A is an integer within the range [−2,147,483,648..2,147,483,647]. Complexity: expected worst-case time complexity is O(N*log(N));. visit 1point3acres.com for more. expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments). Elements of input arrays can be modified. test case (6, [1, 8, -3, 0, 1, 3, -2, 4, 5]) , result = 7
这里要注意找到一对(A[i],A[j])之后, 除非i==j,不然要算两次。例如这里(4,8)和(8,4)。
这里提供两种方法。一种用dict记录key = A element value = the distinct number of A element. 然后循环这个dict就行。第二种就是先sort,然后首尾two pointers,不断向中间靠拢。这里值得注意的就是说重复值,A[i]之后如果有一串重复值,A[j]之后也有一串重复值,那么就要用while判断出各自重复了多少次,然后次数相乘并且乘以2,因为一个pair要算两次,除非i == j.
# you can write to stdout for debugging purposes, e.g. # print "this is a debug message" def solution(K, A): # write your code in Python 2.7 if len(A) < 2: return 0 '''method1 mydict = {} for x in A: if x in mydict: mydict[x] += 1 else: mydict[x] = 1 N = 0 for i in mydict: if K - i > -2147483648: N += mydict[i] * mydict.get(K - i, 0) return N ''' #method 2 A.sort() i,j = 0, len(A) - 1 N = 0 print A while i<=j: sumval = A[i] + A[j] if sumval == K: k1,k2 = i,j if k1 == k2: N += 1 break else: while A[k1] == A[i]: k1 += 1 while A[k2] == A[j]: k2 -= 1 N += (k1 - i) *(j - k2) * 2 i,j = k1, k2 elif sumval < K: i += 1 else: j -= 1 print (A[i],A[j], N) return N
相关文章推荐
- 常用RAID简介
- wait如何处理多进程(多个子进程)
- Factorial Trailing Zeroes
- 深入理解Hadoop YARN中的Container概念
- RAID 图解
- 人工智能有简单的算法吗?Appendix: Is there a simple algorithm for intelligence?
- http://www.nowamagic.net/librarys/veda/detail/1606
- HDU2476 String painter(区间dp)
- 自定义cell的时候出现的错误(用的nib而不是storyboard)invalid nib registered for identifier ((null)) - nib must contain
- 人工智能有简单的算法吗?Appendix: Is there a simple algorithm for intelligence?
- 【杭电】[2057]A + B Again
- Foxmail7.2账号帐号邮件备份和恢复
- EventBus源码解读详细注释(3)PostThread、MainThread、BackgroundThread、Async四种线程模式的区别
- RAID磁盘阵列
- | Error Error occurred running Grails CLI: Profile [org.grails.profiles:base:3.1 .3] declares and in
- Gym 100015B Ball Painting
- 杭电2057A + BAgain
- Ruby on Raisl应用(一):在Rails上配置Mongoid+Mongodb
- public static void main(String[] args) 隐含了什么?
- leetcode011 Container With Most Water