I need help regarding a question which was there in November cook-off 2017:
The problem was pretty simple and my solution for it worked but unfortunately it was not that good in terms of time complexity. Can any one please help and tell how can i reduce time of execution here.(language used: Python 2.7)
if(j!=k and j not in list1 and k not in list1)
Do no use
not in operator, it does a linear search. Use a binary array instead to identify if you already have matched that player or you can use python dictionary, that should do the job too.
int(j) in range(int(k),int(k)+1) and int(k) in range(int(j),int(j)+1)
range() function creates a list that takes linear time and then you use in operator that takes linear time too. instead replace them with relational operators and compute them in constant time.
k <= j <= k
That should do the job
November 20, 2017, 9:22pm
in and not in are O(n), so your solution is O(n^3)
Here’s is my python 3 code