Solution to Abs-Distinct by codility
Question: https://codility.com/demo/take-sample-test/abs_distinct Question Name: AbsDistinct In the original requirement, expected worst-case space complexity is O(N). But actually, O(1) is enought.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | def solution(A): abs_distinct = 1 current = max(abs(A[0]), abs(A[-1])) index_head = 0 index_tail = len(A)-1 while index_head <= index_tail: # We travel the array from the greatest # absolute value to the smallest. former = abs(A[index_head]) if former == current: # Skip the heading elements, whose # absolute values are the same with # current recording one. index_head += 1 continue latter = abs(A[index_tail]) if latter == current: # Skip the tailing elements, whose # absolute values are the same with # current recording one. index_tail -= 1 continue # At this point, both the former and # latter has different absolute value # from current recorded one. if former >= latter: # The next greatest value is former current = former index_head += 1 else: # The next greatest value is latter current = latter index_tail -= 1 # Meet with a new absolute value abs_distinct += 1 return abs_distinct |