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 did something similar. As several others have noted, it's an inefficient approach, but it was really quick to write and handled all the test cases so I considered it fit for purpose.

int chocolateFeast(int n, int c, int m) {
int i,sum,x,j;
sum=n/c;
x=sum;
if(sum==0)
return 0;
while(x>=m)
{
j=x%m;
i=x/m;
sum=sum+i;
x=i+j;
}
return sum;
}

## Chocolate Feast

You are viewing a single comment's thread. Return to all comments →

I guess you are doing way to much unnecessary work. Heree is my while loop

This approach is clean and easy to follow, which is great.

However, it results in O(N) time (input of 10000 1 2 results in the loop running 9999 times.)

If you switch the loop to the following you end up with O( lg(N)) (or 14 in the worst case, since you are using integer math).

Hi Jason I kind of did the same thing what you did but mine didn't pass the test cases how whats the difference

import time

start_time = time.time()

def chocolateFeast1(he_has,cost,wrapper):

start_time = time.time() def chocolateFeast(cash, cost, wrapper_cost):

wrappers = wrappers % wrapper_cost instead of wrappers -= new_chocolate * wrapper_cost

was better understandable for me.

wrappers %= wrapper_cost would be even shorter

thanks.anyways, i got it.

Your code is flawless. Quick, simple and completely readable! Congratulations!

## see this

def chocolateFeast(n, c, m):

Did almost the same thing in Java8

your code will result in performance issues when the count is too high. Thats the reason suggested to do with / and % operators

I did something similar. As several others have noted, it's an inefficient approach, but it was really quick to write and handled all the test cases so I considered it fit for purpose.

int chocolateFeast(int n, int c, int m) { int i,sum,x,j; sum=n/c; x=sum; if(sum==0) return 0; while(x>=m) { j=x%m; i=x/m; sum=sum+i; x=i+j; } return sum; }