We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
O(n) greedy approach that dont need to consider corner cases. We always take the (i-k) choice if possible, if not try (i+k), if that fails return -1.
# Write your code here'''pos[i]=i+kori-kpos[i]=i'''visited=set()res=[]foriinrange(1,n+1):ifi-kinvisitedandi+kinvisited:return[-1]ifi-k>0andi-knotinvisited:res.append(i-k)visited.add(i-k)else:ifi+k>n:return[-1]res.append(i+k)visited.add(i+k)returnres
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Absolute Permutation
You are viewing a single comment's thread. Return to all comments →
O(n) greedy approach that dont need to consider corner cases. We always take the (i-k) choice if possible, if not try (i+k), if that fails return -1.