Codility K complementary pairs

codelity kcomplementary pairs

                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


你可能感兴趣的:(Codility K complementary pairs)