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.
Amazing method. I implemented it in python. I don't know why but many test cases are begin terminated due to timeout. Can anyone help me out ? Thanks.
#!/bin/pythonimportsysQ=int(raw_input().strip())nums=[]# All the query numbersfora0inxrange(Q):N=int(raw_input().strip())nums.append(N)max_nums=max(nums)# Max of all queriesmoves=[-1]*(1+max_nums)# A matrix of moves for all # numbers till max numbermoves[0]=0moves[1]=1moves[2]=2moves[3]=3foriinrange(max_nums+1):if(moves[i]==-1ormoves[i]>moves[i-1]+1):moves[i]=moves[i-1]+1forjinrange(1,i+1):ifj*i>max_nums:breakif(moves[j*i]==-1)or(moves[j*i]>moves[i]+1):moves[j*i]=moves[i]+1fornuminnums:printmoves[num]
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Down to Zero II
You are viewing a single comment's thread. Return to all comments →
Amazing method. I implemented it in python. I don't know why but many test cases are begin terminated due to timeout. Can anyone help me out ? Thanks.