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.
I think they notice that the n k l values are very small, so a bf approach would be good enough to solve the problem, not necessary to find the best solution.
Below is the bf approach I wrote in java passed all cases in less than 0.2s, max is the max value, target can be the median, you just need to loop through all values between target and max value to find the min cost.
INCLUDED INCLUDED INCLUDED
while(max >= target){
belowSum = 0;
aboveSum = 0;
for(int i = 0; i < size; i++){
if(num[i] < max)
belowSum+=(max-num[i]);
else if(num[i] > max)
aboveSum+=(num[i]-max);
}
int temp = aboveSum*k + (belowSum-aboveSum)*l;
if(temp < ans)
ans = temp;
max--;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Beautiful Array
You are viewing a single comment's thread. Return to all comments →
I think they notice that the n k l values are very small, so a bf approach would be good enough to solve the problem, not necessary to find the best solution.
Below is the bf approach I wrote in java passed all cases in less than 0.2s, max is the max value, target can be the median, you just need to loop through all values between target and max value to find the min cost.
INCLUDED INCLUDED INCLUDED
while(max >= target){