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.
This question can be tagged dynamic programming. Because in the main solution part, we memorize the differences. The nutshell of dynamic programming is memorization. It is clear from this slice of code from my solution:
staticfinaldelta[]={0,1,2,5};privatestaticintsolve(inta[]){Arrays.sort(a);intres=Integer.MAX_VALUE;for(intj=0;j<delta.length;j++){intans=0;intd=0;a[0]-=delta[j];for(inti=1;i<a.length;i++){intdiff=a[i]-a[i-1];// refer to prev elementsans+=getMinSteps(diff+d);d+=diff;// memorizing diffs}a[0]+=delta[j];res=Math.min(res,ans+(delta[j]==0?0:1));}returnres;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Equal
You are viewing a single comment's thread. Return to all comments →
This question can be tagged dynamic programming. Because in the main solution part, we memorize the differences. The nutshell of dynamic programming is memorization. It is clear from this slice of code from my solution: