我正在学习Codility Counting Lesson(https://codility.com/media/train/2-CountingElements.pdf),我需要帮助才能理解最快的解决方案。
我想知道计数功能的含义:
count = counting(A, m)
问题:
给出一个整数m(1 def fast_solution(A, B, m):
n = len(A)
sum_a = sum(A)
sum_b = sum(B)
d = sum_b - sum_a
if d % 2 == 1:
return False
d //= 2
count = counting(A, m)
for i in xrange(n):
if 0 <= B[i] - d and B[i] - d <= m and count[B[i] - d] > 0:
return True
return False
答案 0 :(得分:2)
计数在本文前面定义,实现如下:
def counting(A, m):
n = len(A)
count = [0] * (m + 1)
for k in xrange(n):
count[A[k]] += 1
return count
它只计算每个元素在数组中出现的次数。