You are viewing a single comment's thread. Return to all comments →
python
# P 1 based indexing # hashMap for 1 to N values (P) # |pos[i]-i|=k # pos[i]-i=-k pos[i]-i=k # pos[i]=-k+i pos[i]=k+i # -1 3 # 0 4 # 1 5 # 2 6 # k=4 iValues={} posValues={} for i in range(1,n+1): iValues[i]=(-k+i,k+i) posValues[i]=False result = [] #iValues for i in range(1,n+1): if(iValues[i][0]>0 and posValues[iValues[i][0]]==False): posValues[iValues[i][0]]=True result.append(iValues[i][0]) elif(iValues[i][1]<=n and posValues[iValues[i][1]]==False): posValues[iValues[i][1]]=True result.append(iValues[i][1]) else: return [-1] return result #spaceComplexity 0(n) #runtimeComplexity 0(n)
Absolute Permutation
You are viewing a single comment's thread. Return to all comments →
python