Problem:
We want make a package of goal kilos of chocolate. We have small bars (1 kilo each) and big bars (5 kilos each). Return the number of small bars to use, assuming we always use big bars before small bars. Return -1 if it can't be done.
makeChocolate(4, 1, 9) → 4
makeChocolate(4, 1, 10) → -1
makeChocolate(4, 1, 7) → 2
Solution:
public int makeChocolate(int small, int big, int goal) { int rem = goal % 5; if (small + (big*5) < goal) return -1; else if (rem <= small && goal - big*5 > 4) return rem + 5; else if (rem <= small) return rem; else return -1; }
public int makeChocolate(int small, int big, int goal) {
ReplyDeleteint a = (goal-5*big);
if (a<=small && a>=0) return a;
if (a<0 && goal%5<=small) return goal%5;
return -1;
}
public int makeChocolate(int small, int big, int goal) {
ReplyDeleteif (big * 5 + small < goal | small < goal % 5) return -1;
if (big * 5 <= goal) return goal - big * 5;
return goal % 5;
}
public int makeChocolate(int small, int big, int goal) {
ReplyDeleteif ((small + big * 5) < goal) return -1;
int smallPacks = goal - (Math.min(goal / 5, big) * 5);
return smallPacks > small ? -1 : smallPacks;
}
public int makeChocolate(int small, int big, int goal) {
ReplyDeleteint bigReq = goal / 5;
if (big <= bigReq)
goal -= big * 5;
else
goal -= bigReq * 5;
return small >= goal ? goal : -1;
}
public int makeChocolate(int small, int big, int goal) {
ReplyDeleteint big_t = big * 5;
if (goal > big_t + small) {
return -1;
} else if (goal / 5 > big && goal % 5 > small) {
return -1;
} else if ((goal - (goal / 5 * 5)) > small) {
return -1;
} else {
if (goal / 5 > big) {
return goal - big_t;
} else {
return goal - (goal / 5 * 5);
}
}
}
int ones = (goal/5>=big&&(goal-big*5)<=small)?
ReplyDelete(goal-big*5):
(goal/5<big&&goal%5<=small)? (goal%5) : -1;
return ones;
public int makeChocolate(int small, int big, int goal) {
ReplyDeleteint numBig = big;
while (numBig*5 > goal){
numBig--;
}
if (numBig > big){
numBig = big;
}
int rem = goal - (numBig*5);
if (small >=rem){
return rem;
}
return -1;
}
correction. superfluous code removed:
Deletepublic int makeChocolate(int small, int big, int goal) {
int numBig = big;
while (numBig*5 > goal){
numBig--;
}
int rem = goal - (numBig*5);
if (small >=rem){
return rem;
}
return -1;
}
public int makeChocolate(int small, int big, int goal) {
ReplyDeleteint i=goal%5;
int j=goal/5;
int k=(big*5)-goal;
if((big*5)+small<goal)return -1;
if((j<=big)&&(i<=small))return i;
if((big*5)<goal)
{
if(Math.abs(big*5-goal)<=small)return Math.abs(big*5-goal);
}
return -1;
}
public int makeChocolate(int small, int big, int goal) {
ReplyDeleteint big_t = big * 5;
if (goal > big_t + small) {
return -1;
} else if (goal / 5 > big && goal % 5 > small) {
return -1;
} else if ((goal - (goal / 5 * 5)) > small) {
return -1;
} else {
if (goal / 5 > big) {
return goal - big_t;
} else {
return goal - (goal / 5 * 5);
}
}
}
public int makeChocolate(int small, int big, int goal) {
ReplyDeleteif (0 <= (goal - 5 * big) && (goal - 5 * big) <= small){
return (goal - 5 * big);
}
if ((goal - 5 * big) < 0 && (goal % 5) <= small){
return (goal % 5);
}
return -1;
}
With above code, it will fail for this testcase makeChocolate(60, 100, 550) → 50
ReplyDeleteLittle modification to above method
public int makeChocolate(int small, int big, int goal) {
int rem = goal%5;
if(goal > small+big*5)
return -1;
else if (rem <= small && goal - big*5 > 4)
return goal - big*5;
else if(rem<=small)
return rem;
else
return -1;
}
public int makeChocolate(int small, int big, int goal) {
ReplyDeletereturn(goal-5*big>small)?-1:(goal-5*big==0)?0:(goal-5*big>0)?goal-5*big:(goal%5<=small)?goal%5:-1;
}
public int makeChocolate(int small, int big, int goal) {
ReplyDeleteint large = big * 5;
if (goal >= 10) {
int w = goal - large;
if (w <= small) return w;
else return -1;
}
if (goal % 5 <= small) return goal % 5;
return -1;
}
public int makeChocolate(int small, int big, int goal) {
ReplyDeleteint count=goal%5;
if(small+big*5big*5 && goal%5==0){
return goal-big*5;
}
else if(count<=small && goal-big*5>4) return count+5;
else if(count<=small) return count;
return -1;
}
int res=0;
ReplyDeleteint i=0;
if(goal>big*5+small)
return -1;
while(res<=goal && igoal) res=res-5;
if(goal-res>small)return-1;
return(goal-res);
if (small + big * 5 < goal) return -1;
ReplyDeleteint numBig = (big * 5 > goal) ? ((int) Math.floor(goal/5)) : big;
if (goal - numBig*5 <= small) return goal - numBig*5;
return -1 ;
public int makeChocolate(int small, int big, int goal) {
ReplyDeleteif (small + 5 * big < goal) {
return -1;
} else if (5 * big < goal && goal-5*big <= small) {
return goal-5*big;
} else if (5 * big > goal && goal%5 <= small) {
return goal%5;
} else if (5 * big == goal) {
return 0;
} else {
return -1;
}
}
public int makeChocolate(int small, int big, int goal) {
ReplyDeleteif(big*5==goal) return 0;
for(int i =0; i<=big ; i++){
for (int j =0; j <= small ; j++){
if ((i*5) == goal){
return 0;}
else if(j>5){
if ((big*5)+j == goal){
return j;
}
}
else if(((i*5)+j)==goal){
return j;}
}
}
return -1;
}
public int makeChocolate(int small, int big, int goal) {
ReplyDeleteint bigTotal = big * 5;
while (bigTotal > goal) {
bigTotal -= 5;
}
int amount = goal - bigTotal;
if (amount <= small) {
return amount;
}
else {
return -1;
}
}
public int makeChocolate(int small, int big, int goal) {
ReplyDeleteif((big*5)+small < goal)
return -1;
if(goal%5 > small)
return -1;
if(big*5>goal)
return goal%5;
else return goal - (big*5);
}
public int makeChocolate(int small, int big, int goal) {
ReplyDeletefor (int i = 0; i <= big; i++) {
boolean isValid = (i * 5 == goal);
if (i * 5 > goal) {
big = i - 1;
}
if (isValid) return 0;
}
for (int i = 0; i <= small; i++) {
if ((big * 5) + i == goal) {
return i;
}
}
return -1;
}