Greedy thief
Description:
When no more interesting kata can be resolved, I just choose to create the new kata, to solve their own, to enjoy the process -- myjinxin2015 said
Description:
In a dark, deserted night, a thief entered a shop. There are some items in the room, and the items have different weight (in kg) and price (in $).
The thief is greedy, his desire is to take away all the goods. But unfortunately, his bag can only hold n
kg of items. So he has to make a choice, try to take away more valuable items.
Please complete the function to help the thief to make the best choices. Two arguments will be given: items
(an array that contains all items) and n
(the maximum weight the package can accommodate).
The list of items is provided as an array of objects:
[
{weight:2, price:6},
]
The result should be a list of the original items that the thief should take away and that has the maximum possible total price.
Notes:
- Order of the items in the output do not matter
- If more than one valid solutions exist, you should return one of them
- If no valid solution should return
[]
- You should not modify argument
items
or the items themselves (in languages where they are mutable). - Pay attention to performance: the thief doesn't have all night to decide what to take or not!
Examples
For a list of the following available items:
weight | price |
---|---|
2 | 6 |
2 | 3 |
6 | 5 |
5 | 4 |
4 | 6 |
... and with a maximum weight of n=10
, the best option could be a total price of 15$
, collecting the following items:
weight | price |
---|---|
2 | 6 |
2 | 3 |
4 | 6 |
More examples please see the example tests ;-)
Similar Kata:
Stats:
Created | Nov 14, 2016 |
Published | Nov 15, 2016 |
Warriors Trained | 1865 |
Total Skips | 65 |
Total Code Submissions | 7469 |
Total Times Completed | 281 |
JavaScript Completions | 171 |
Haskell Completions | 6 |
Python Completions | 115 |
Total Stars | 129 |
% of votes with a positive feedback rating | 91% of 69 |
Total "Very Satisfied" Votes | 58 |
Total "Somewhat Satisfied" Votes | 10 |
Total "Not Satisfied" Votes | 1 |
Total Rank Assessments | 5 |
Average Assessed Rank | 4 kyu |
Highest Assessed Rank | 4 kyu |
Lowest Assessed Rank | 5 kyu |