Question

In: Computer Science

We have a bag which has a weight capacity of x kilograms. We also have n...

We have a bag which has a weight capacity of x kilograms. We also have n
items, each of different weights. Our target is to pack as many items as possible in
the bag, without exceeding the weight capacity. Note that, we want to maximize
the number of items, not the total weights of the items. Design an algorithm to
determine the desired items in O(n) time.

Solutions

Expert Solution

We assume w[] is the array of weights sorted in ascending order and subscripted from 0 to n-1.

ALGORITHM:

1. We input x, n and w[].

2. We set bag to empty, i to 0 and wCurr to 0.

3. We repeat steps 4 and 5 till i<n.

4. If wCurr+w[i]<=x, we add the ith item to the bag, make wCurr=wCurr+w[i] and increment i by 1.

5. Else we go to step 6.

6. We output the bag, wCurr and i. wCurr is the weight of the bag, and i is the number of items in the bag.

In its worst case, it will go through all the items, i.e., a time complexity of O(n).


Related Solutions

We have a bag that contains n red balls and n blue balls. At each of...
We have a bag that contains n red balls and n blue balls. At each of 2n rounds we remove one of the balls from the bag randomly, and place it in one of available n bins. At each round, each one of the balls that remain in the bag is equally likely to be picked, as is each of the bins, independent of the results of previous rounds. Let Nk be the number of balls in the k-th bin...
Mean net weight of a sample of bag of chips is 301g (n = 10), but...
Mean net weight of a sample of bag of chips is 301g (n = 10), but the company’s claim is 300g. Find out if our sample is statistically different from the company’s claim. Estimated SD = 0.523. Write an interpretation
Let X be the weight of a randomly selected 10oz bag of chips. Suppose that X...
Let X be the weight of a randomly selected 10oz bag of chips. Suppose that X has a normal distribution with a mean of 10.2 and standard deviation of .05. Find the weight of x* so that 95% of all 10oz bags have a weight of at least x*.
A satellite which has a weight of 35000 N on the surface of the earth is...
A satellite which has a weight of 35000 N on the surface of the earth is moving in a circular orbit around the earth. The radius of the orbit is 2R where R approx  6,400 km is the radius of the earth. Derive and find the period of the satellite from Newton’s second law. What is the angular momentum of the satellite as it orbits the earth? If we now desire to push the satellite into a higher orbit with a...
We have a bag filled with 201 marbles, of which 100 of them are blue and...
We have a bag filled with 201 marbles, of which 100 of them are blue and 101 of them are red. Every turn, we remove 2 marbles from the bag. If the two marbles are of the same color, we remove the two marbles but add a blue marble into the bag. If the two marbles are of different colors, we remove the two marbles and add a red marble into the bag. What is the color of the last...
a packing plant fills bags with cement. the weight X kg of a bag can be...
a packing plant fills bags with cement. the weight X kg of a bag can be modeled normal distribution with mean 50kg and standard deviation 2kg. a) Find the probability that a randomly selected bag weighs more than 53 kg. b)find the weight that exceed by 98% of the bags c)3 bags are selected randomly. Find the probability that two weigh more than 53 kg and one weigh less than 53kg
We have a random sample of size n (which is large), and we wish to test...
We have a random sample of size n (which is large), and we wish to test - Ho: X~UNIF(0,1) vs Ha: X~exp(1). How would you conduct hypothesis testing? Describe procedure.
. Suppose 2% of people have Syndrome X. We have a Syndrome X detecting test which...
. Suppose 2% of people have Syndrome X. We have a Syndrome X detecting test which gives which gives a positive result for 90% of people who do have the syndrome, but also gives a positive result for 10% of people who don’t actually have the syndrome. A patient comes in and gets a positive result. What are the chances they have Syndrome X? For full credit, you must show your work.
Suppose we have the following vectorial equation c = na x b where n is a...
Suppose we have the following vectorial equation c = na x b where n is a constant, and c, a, b are vectors Givens n = -1 a = 2i – j a) Determine the direction and size of the vector c if b = 2i b) Determine the direction and size of the vector c if b = 2k c) Determine the direction and size of the vector c if b = 2i – k I really need the...
Suppose you have N items with values xi from which we want to sample n items...
Suppose you have N items with values xi from which we want to sample n items with replacement. Each item has its own probability pi of being selected across all times we pick items. What's the estimated variance of the Horvitz-Thompson estimator of T? Let T = > 1, 1=1
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT