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.
Hello, here is my solution. It didn't passed the last test case, but it's a good path of understanding and how you can implement yours one.
intmin(inta,intb){returna>b?b:a;}introunds(intele,inttarget,int**memo){if(ele<target)returnINT_MAX;if(ele==target)return0;if(memo[ele][target]!=-1)returnmemo[ele][target];intresult=1+min(min(rounds(ele-5,target,memo),rounds(ele-2,target,memo)),rounds(ele-1,target,memo));returnmemo[ele][target]=result;}intequal(intn,int*arr){intMIN=INT_MAX;intmax=INT_MIN;/** * We will find the max element * as we need to create our memoization * table *//** * First of all find the minimum * value in the array so we can start * reducing the values to matche it */for(intx=0;x<n;x++){if(arr[x]<MIN)MIN=arr[x];if(arr[x]>max)max=arr[x];}/** * Create the memoization * table to save our computed results * memo[element][target] */int**memo=calloc(max+1,sizeof(int*));for(intx=0;x<=max;x++){memo[x]=calloc(max+1,sizeof(int));}for(intx=0;x<=max;x++){for(intl=0;l<=MIN;l++){memo[x][l]=-1;}}/** * Now starting cutting the array * to matche the minimu value */intmin_rounds=0;intmin_rounds_zero=0;for(intx=0;x<n;x++){min_rounds+=rounds(arr[x],MIN,memo);min_rounds_zero+=rounds(arr[x],0,memo);}for(intx=0;x<=max;x++){free(memo[x]);}free(memo);returnmin(min_rounds,min_rounds_zero);}
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 →
Hello, here is my solution. It didn't passed the last test case, but it's a good path of understanding and how you can implement yours one.