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.
publicstaticintDownToZero(intn){returnnewDownToZeroSolution().Solve(n);}classDownToZeroSolution{staticintcalculated_N;staticreadonlyint[]solutions;staticDownToZeroSolution(){calculated_N=1;solutions=newint[1_000_001];solutions[1]=1;}publicintSolve(intn){if(n<=calculated_N)returnsolutions[n];for(inti=calculated_N+1;i<=n;i++){solutions[i]=CalculateItem(i);}calculated_N=n;returnsolutions[n];}staticintCalculateItem(intn){intmin=solutions[n-1];intd_start;intd_step;if((n&1)==1){d_start=3;d_step=2;}else{d_start=2;d_step=1;}inttopN=(int)(Math.Sqrt(n)+0.1);// n = d1*d2for(intd1=d_start;d1<=topN;d1+=d_step){if(n%d1==0)min=Math.Min(min,solutions[Math.Max(d1,n/d1)]);}return1+min;}}
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 →
Here is my DP solution (C# code). Take a look: