Google | Onsite | Gems
Anonymous User
3418

There are N kinds of gems in a mine.
Each gem has a weight of 1.
Their count and price (per gem) is given as follows:
c[0], c[1], .... c[N-1]
p[0], p[1], .... p[N-1]

You have a knapsack of weight W.
Maximize the total price of gems that you can keep in the knapsack.

Solution: Simple Greedy approach. Sort the gems w.r.t. price from highest to lowest and keep them in the knapsack until it is full.

Follow-up:

If c and p are given in the form of running queries, maximize the total price after each query.

Comments (5)