Question

Solve in Java and Python public static int feedCows(int[] cows, int k) You are a farmer...

Solve in Java and Python
public static int feedCows(int[] cows, int k)

You are a farmer with a row of cows that need feeding and an unlimited supply of food at the beginning of the row of cows. The cows are represented as an array, ​cows​, where ​cows[i]​ is the non-negative amount of cups of food that cow needs. Capacity, k, is the amount of cups of food you can hold in your bucket. You have to feed all the cows. Assume you start with a full bucket of food and at the position of the unlimited supply of food.

Return the minimum number of steps needed to feed the cows with the following constraints:

  1. Each cow is 1 step apart from each other.

  2. You must finish feeding cows[i-1] before moving to cows[i]. That is, you cannot

    skip cows while walking.

  3. You must use all the food in your bucket. Until then, you cannot go any farther.

    That is, if you have 2 cups of food in your bucket, you must go to the next cow before returning to refill the bucket. Also, if your bucket is at 0 cups and you just finished feeding cows[i], you still need to travel to the next cow (if it exists) as the next cow may not need food i.e. cows[i] = 0.

  4. The unlimited supply of food is located 1 step before the beginning of the array of cows. So if you are traveling with an empty bucket from index 2 to refill, you must travel 3 steps back to get to the supply of food.

Sample input/output​:

  1. input: cows = [1, 2, 3, 4], k = 5

    output: 10
    Explanation: Start with a full bucket. Feed cows[0], bucket is at 5-1=4, steps is

    0+1=1. Feed cow[1] bucket is at 4-2=2, steps is 1+1=2. Feed cows[2] as much as is in the bucket, so bucket is at 0, steps is 2+1=3. Go back to the supply of food, REFILL and RETURN to cows[2]. So steps 3+3+3=9. cows[2] still needs 1 cup of food. After feeding cows[2], your bucket is at 5-1=4, steps is at 9. Feed cows[3], bucket is at 0, steps is at 10. All cows have been fed. Do not need to step away from the last cow.

  2. input: cows = [1, 2, 3, 4, 5] k = 5 output: 21

    Explanation: Same as before after feeding the cow[4], the bucket is at 4-4=0 cups. Must step to cows[4] to see if it needs to be fed, steps is 10+1=11. You see cows[4] needs to be fed so you go back to the supply of food to REFILL your bucket and return to cows[4]. So the bucket is at 5 and steps is 11 + 5 (go to supply) + 5 (go back to cows[4]) = 21. Feed cows[4] and you are done. Return 21.

0 0
Add a comment Improve this question Transcribed image text
Answer #1


public class Main {

   public static void main(String[] args)
   {
       int[] cows = { 1, 2, 3, 4,5};
       System.out.println(feedCows(cows, 5));
   }

   public static int feedCows(int[] cows, int k)
   {
       int steps = 0;
       int contentInBucket = k;
       int bucketPosition = 0;
       boolean finished = false;
       for (int i = 0; i < cows.length;)
       {
           steps = steps+(i + 1) - bucketPosition;
           if (contentInBucket > cows[i])
           {
               contentInBucket = contentInBucket - cows[i];
               cows[i] = 0;
               bucketPosition = (i + 1); //index starts at 0 so we have to add 1 everywhere to i
           }
           else if (contentInBucket == cows[i])
           {
               cows[i] = contentInBucket = 0;
               bucketPosition = i+1;
               bucketPosition = i+2;   
           }
           else
           {
               cows[i] = cows[i] - contentInBucket;
               contentInBucket = 0;
               bucketPosition = (i + 1);
           }
           if(i==cows.length-1&&cows[i]==0)
           {
               finished = true;
           }
           if(cows[i]==0&&contentInBucket==0&&!finished) //current cow fed completely had to move to next cow
           {
               steps = steps+1;
           }
           if (contentInBucket == 0 && !finished)
           {
               steps = steps + bucketPosition;
               bucketPosition = 0;
               contentInBucket = k;
           }
           if(cows[i]==0) {
               i++;
           }
       }

       return steps;
   }
}

Add a comment
Know the answer?
Add Answer to:
Solve in Java and Python public static int feedCows(int[] cows, int k) You are a farmer...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Farmer DJ knows that feeding a grain supplement to his cows will produce more milk according...

    Farmer DJ knows that feeding a grain supplement to his cows will produce more milk according to the schedule given below.(Note: the number on the top is the total pounds of supplement used and the number on the bottom is the total pounds of milk produced. For example, one pound of supplement produces 5 pounds of milk, two pounds of supplement produces 8 pounds of milk, and so on.) otal pounds of supplement used 1 2 3 4 5 Total...

  • 3) A dairy man thinks that the average weight gain of his cows depends on two factors: the type of grain which they are fed and the type of grass which they are fed. The dairyman has four differe...

    3) A dairy man thinks that the average weight gain of his cows depends on two factors: the type of grain which they are fed and the type of grass which they are fed. The dairyman has four different types of grain from which to choose and three different types of grass from which to choose. He would like to determine if there is a particular combination of grain and grass whi would lead to the greatest weight gain on...

  • Please Help! Need in Java C++ Object oriented programming. Thank you! Method 2: public static int...

    Please Help! Need in Java C++ Object oriented programming. Thank you! Method 2: public static int sumCapped(int[] nums, int x) This method will return the largest sum that is less than or equal x found in one pass of the array. This means that you must check each number in succession to determine whether or not you should add it in. For example, for the array {4, 2, 3, 5} with the value of the paramter x set to 7,...

  • 1. (37 points) Lolita is the intelligent and charming Holstein cow, who has been instructed in...

    1. (37 points) Lolita is the intelligent and charming Holstein cow, who has been instructed in the mysteries of microeconomics. It turns out that she now considers herself more as a profit-maximizing firm than as a simple consumer: she uses (eats) cow feed and hay to produce milk. If m represents her production of milk, x her consumption of cow feed, and y her con- sumption of hay, Lolita’s technology can be summarized by the following production function: m =...

  • In Java, Using arrays. You are going to write a program that will play a solitaire...

    In Java, Using arrays. You are going to write a program that will play a solitaire (one-player) game of Mancala (Links to an external site.) (Links to an external site.). The game of mancala consists of stones that are moved through various buckets towards a goal. In this version of mancala, the user will be able to choose a number of buckets and a number of stones with which to set up the game. The buckets will be created by...

  • The table shows the production function for snack cakes and labor. Assume fixed costs are $200/day...

    The table shows the production function for snack cakes and labor. Assume fixed costs are $200/day and that one full time worker costs $80/day ($10x8hrs). Given this information, what is the total cost to produce 45 cases? (assume labor is your only variable cost) # of Workers Q of Snack Cakes TVC TFC TC MC 0 0 1 5 1.6 10 2 15 2.35 20 2.75 25 3.22 30 3.75 35 4.35 40 5 45 5.75 50 6.6 55 7.5...

  • Urgent help needed related python ! Thanx *On average the population increases by 30% each year...

    Urgent help needed related python ! Thanx *On average the population increases by 30% each year due to immigration and natural births. * Every 18 years the population is reduced by 40% due to children coming of age and being drafted for football. Year O is considered a draft year. The calculation for draft numbers is done after the calculation for population growth. * Each unit of food is enough to feed a single person for an entire year. *...

  • 3. Consider the mystery method given. public static int mystery ( int n) [ if (n...

    3. Consider the mystery method given. public static int mystery ( int n) [ if (n == 0 ) { return 1; How do we get the values for recurse? else if (n%2 == 0 ) { int recurse = mystery ( n - 1); int result = recurse + n; return result; since n =5, we go to the else statement and do int recurse = mystery(5-1) which equals 4? why is 3 written? else { int recurse =...

  • You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement...

    You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement this method return new int[] {}; }    There is a utility method provided for you with the following signature. You may use this method to convert a list of integers into an array. public static int[] convertIntegers(List<Integer> integers) Provided code import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class MatrixSearch { // This method converts a list of integers to an array...

  • You are in charge of a combustion furnace. The furnace is fed an air stream and...

    You are in charge of a combustion furnace. The furnace is fed an air stream and a methane stream which is combusted to produce heat that is used to boil water and produce steam for your plant. The combustion of fuels using an air stream which contains nitrogen generally produces significant amounts of NO, compounds which are an air pollutant, so the exhaust gases must generally be removed through various processes. In this case, your furnace exhaust is passed through...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT