You are given two lists A and B, both of which contain a permutation of the numbers 1 \dots n (i.e., each list contains the numbers 1 \dots n in some order).
Your task is to count, how many of the numbers 1 \dots n occurs on the list A earlier than it occurs on the list B. The goal is an algorithm with time complexity O(n).
In a file permutations.py
, implement a function count
that returns the desired count.
def count(a, b): # TODO if __name__ == "__main__": print(count([1,2,3], [1,2,3])) # 0 print(count([2,3,4,1], [1,2,3,4])) # 3 print(count([4,7,3,1,6,2,5], [5,6,1,2,4,3,7])) # 3 print(count([5,4,9,1,8,3,2,6,7], [6,2,8,4,9,1,5,7,3])) # 5
Explanation: In the first example, both lists are the same and no number occurs earlier on A than on B. In the second example, the numbers 2, 3 and 4 occur on A at a smaller index than they occur on B.