You have a bag that can hold 100kg of fruit. You are in a shop that sells (an unlimited supply of) pumpkins, watermelons, and squash, weighing 10, 2, and 1kg each, respectively. Give a greedy algorithm to fill your bag with the smallest number of fruits/vegetables (eg, buying a watermelon is preferable to buying two squash, etc). The total weight has to be exactly 100kg. Argue that this will return you the optimal solution. Argue that, given this fruit/vegetable collection, you can completely fill a bag of any weight capacity (ie, you can realize any weight, not just 100kg).
Solution:
As given in the problem, we have an unlimited amount of supply.
This means that we can take as many weighted items we can and then rest of the items with smaller weight in the decreasing order in order to produce the minimum number of fruits.
the algorithm is given below:
The given algorithm is first filling all the most weighted fruits/vegetables in the bag then the next weighted item is placed in the bag, the given approach is a greedy approach and it will always produce an optimal solution.
Suppose the bag weight is 113 then our algorithm will return
113/10= 11
numberOfFruits = 11
then
113%10= 3
numberOfFruits = 11 + (3/2)= 11 + 1= 12
then 1 is remining
which means
numberOfFruits = 12 + 1= 13
Please, please upvote and ask your doubts in the comments.
You have a bag that can hold 100kg of fruit. You are in a shop that...
Assume that you have 6 different types of vegetables and 9 different types of fruit. A vegetable salad consists of a mixture of any 3 types of vegetables, and a fruit salad consists of a mixture of 3 kinds of fruit.(1) In how many different ways can you prepare a vegetable salad and a fruit salad? (2) In how many different ways can you prepare a vegetable salad or a fruit salad but not both? (3) How many ways can...
Could you convert this PDF to HTML format using styles for massgeneral.org ? Here’s a good example of a page you can copy HTML from:http://www.massgeneral.org/children/turner-syndrome/turner-syndrome-mosaic.aspx. When you’re done, you can send me the HTML and I’ll pop it into a site template and we can take a look. PS You don’t have to build anything from scratch for this—it’s just about tagging the subheads and the bulleted lists, etc. and getting that “Did you know?” box into the same format...
Need answers. thank you VOCABULARY BUILDER Misspelled Words Find the words below that are misspelled; circle them, and then correctly spell them in the spaces provided. Then fill in the blanks below with the correct vocabulary terms from the following list. amino acids digestion clectrolytes nutrients antioxident nutrition basal metabolic rate extracellulare oxydation calories fat-soluble presearvatives catalist glycogen processed foods cellulose homeostasis saturated fats major mineral coenzyeme trace minerals diaretics metabolism water-soluable 1. Artificial flavors, colors, and commonly added to...