Sample Java Code (makeChocolate at codingbat)

The problem here in bat java coding made me stretch a bit:

Problem:

We want to make a package of whole kilograms of chocolate. We have small bars (1 kilogram each) and large bars (5 kilograms each). Return the number of small bars to use, assuming we always use large bars in front of small bars. Return -1 if this is not possible.

makeChocolate (4, 1, 9) β†’ 4 makeChocolate (4, 1, 10) β†’ -1 makeChocolate (4, 1, 7) β†’ 2

http://codingbat.com/prob/p191363

I could come up with this as a solution, I'm not sure if this is the best way that any enthusiast would like to consider, with great pleasure! I typed this without any compiler or anything, so excuse me for the indentation:

ANY BEST ANSWERS WILL BE RECOGNIZED:

public int makeChocolate(int small, int big, int goal) { if(goal<5&&small>=goal) { return goal; } if(goal<5&&small<goal) { return -1; } if(goal>=5) { if(5*big>goal) { int temp=goal/5; if(goal-temp*5<=small) { return goal-temp*5; } if(goal-temp*5>small) { return -1; } } if(5*big<goal) { if(small<(goal-5*big)) { return -1; } if(small>=(goal-5*big)) { return goal-5*big; } } } return 0; } 
+6
source share
15 answers
 private final int BIG_SIZE = 5; public int makeChocolate(int small, int big, int goal) { int s = goal - Math.min(big, goal / BIG_SIZE ) * BIG_SIZE ; return (s <= small) ? s : -1; } 

Explanation:

goal / BIG_SIZE calculates the total number of large bars. Of course, it can be more than ours, so Math.min(big, goal / BIG_SIZE) limit this number to big (we have the number of large bars).

If we multiply this number by the size of a large bar (5 kilograms) and subtract from goal , we get the number of remaining kilograms filled with small bars ( s ).

The second line just checks to see if we have enough small bars. If yes, s is the result, if not, we cannot make this chocolate ( -1 ).

+19
source
 private final int BIG_SIZE = 5; public int makeChocolate(int small, int big, int goal) { // 1 while (goal >= BIG_SIZE && big >= 1) {goal -=BIG_SIZE; big -= 1;} // 2 return goal <= small ? goal : -1; } 

I think this is a clear solution:

  • Use all the great candies
  • Check if we have enough small chocolate and return value
+4
source

Here is a simple solution using only logical logic and simple math. Once a goal failure occurs, the problem is easier to resolve.

 public int makeChocolate(int small, int big, int goal) { big *= 5; if (big + small < goal || small < goal%5) return -1; //goal can not be met small = goal - big; //use big bars first return small < 0 ? (big+small)%5 : small; } 
+2
source

Here, my solution uses a while loop (instead of calling the method recursively):

 public int makeChocolate(int small, int big, int goal) { while (goal >=5 && big>0) { goal -= 5; big--; } if (goal<=small) { return goal; } else return -1; } 

1. Abstract 5 until the rest is between 0 and 4 inclusive.

2.Stop subtracts if the number of large bars has disappeared.

3. Check if there are enough small bars to reach the goal.

4. Repeat rest, otherwise -1

+1
source
 public int makeChocolate(int small, int big, int goal) { int nbSmallNeeded; int nbBigNeeded = Math.abs(goal/5); if(big >= nbBigNeeded){ nbSmallNeeded= goal - nbBigNeeded*5; } else { nbSmallNeeded = goal - 5*big; } return small >= nbSmallNeeded? nbSmallNeeded: -1; } 
+1
source
 private final int BIG_SIZE = 5; public int makeChocolate(int small, int big, int goal) { if(big > 0 && goal >= BIG_SIZE ) return makeChocolate(small, --big, goal-BIG_SIZE); return (small >= goal ? goal : -1); } 

Brief explanation:

While we can still use large chocolate (ie #big> 0 & target> BIG_SIZE), the call method recursively discards one large chocolate bar and target BIG_SIZE.

Once we can no longer use the big one, try to check if the small ones are enough to cover the remaining target.

0
source
 public int makeChocolate(int small, int big, int goal) { int b=big*5; if(b>0 && b+small>=goal && goal%5<=small || small>=goal) { while(goal>=5 && b>0) { goal-=5; b-=5; } return goal; } return -1; } 

β€œif” to check if big and small chocolate are enough, and β€œbye” to get rid of the right amount of big chocolates

0
source
 public int makeChocolate(int small, int big, int goal) { //fail number 1 -- not enough chocolate to reach the goal. if(goal > big*5 +small) return -1; //fail number 2 -- not enough small ones to fill the remaining. if(goal%5 > small) return -1; //fail number 3 -- not enough big ones if(goal - big*5 >=5) return goal-big*5; return goal%5; } 

I believe that codingbat tries to solve the problem without using any form of loops only if it refers to the previous MakeBrics exercise. His strategy is to find simple ways to verify failure. An easy way to look at this is to see it as a screening process.

Failure 1 - It makes no sense to try to calculate how much we need if we don’t have enough chocolate to start with. It takes time and is inefficient.

Crash 2 - We check to see if there are enough small bars to fill the remaining space. If this is not a termination operation, since it makes no sense to calculate further ones.

Failure 3 - In the case when we are not large enough to blindly rely on the operation of the module below, whether it was supposed to be infinite large, so it gives only rest, we only need to check how much we do have large. Since failure number 1 has passed, we know that we have enough chocolate to fill the order, so all we need to do is sum up the big ones and then subtract from the goal, which leaves us with the right answer for the little ones.

0
source
 public int makeChocolate(int small, int big, int goal) { int goalDiv = goal / 5; int goalRem = goal % 5; // Equal or more big bars, and small bars available if (goalDiv <= big && goalRem <= small) return goalRem; // Not enough big bars, but more than goal when combined with small bar if (goalDiv > big && (big * 5 + small >= goal)) return goal - (big * 5); return -1; } 
0
source
 public int makeChocolate(int small, int big, int goal) { if(goal > (big * 5)) { if( (goal - 5 * big) <= small ) return goal - 5 * big; } else { if(goal % 5 <= small) return goal % 5; } return -1; } 
0
source

Here is my code:

 public int makeChocolate(int small, int big, int goal) { goal=goal-big*5; while(goal<0){ goal+=5; } if(small>=goal){ return goal; } else{ return -1; } } 
0
source
 public int makeChocolate(int small, int big, int goal) { if(goal/5 <= big) goal = goal - (goal/5)*5; else goal = goal - big*5; if(goal <= small) return goal; return -1; } 
0
source
 public int makeChocolate(int small, int big, int goal) { int bigs = big*5; while(bigs > goal) { big--; bigs = big*5; } int compare = goal - bigs; if(small >= compare) return compare; if(small < compare) return -1; return small; } 
0
source
  public int makeChocolate(int small, int big, int goal) { int big_amount = 0; for (int x = 1; x * 5 <= goal; x++){ if (big > big_amount) big_amount++; } if (goal - big_amount*5 > small) return -1; else return goal - big_amount*5; } 
0
source
 public int makeChocolate(int small, int big, int goal) { // Condtion When Fails if (goal > 5 * big + small || goal % 5 > small) { return -1; } if (goal > 5 * big) { return (goal - 5 * big); } else { return (goal % 5); } } 
0
source

Source: https://habr.com/ru/post/957922/


All Articles