码迷,mamicode.com
首页 > 其他好文 > 详细

[LintCode] BackPack Dynamic Programming Problems

时间:2017-06-01 13:20:03      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:strong   log   tps   lint   lte   ram   ide   hal   alert   

This blog talks about using dynamic programming to solve the famous back pack and its variant problems.

BackPack I

Given n items with size Ai, an integer m denotes the size of a backpack. How full you can fill this backpack?

 Notice

You can not divide any item into small pieces.

Example

If we have 4 items with size [2, 3, 5, 7], the backpack size is 11, we can select [2, 3, 5], so that the max size we can fill this backpack is 10. If the backpack size is 12. we can select [2, 3, 7] so that we can fulfill the backpack.

You function should return the max size we can fill in the given backpack.

Challenge 

O(n x m) time and O(m) memory.

O(n x m) memory is also acceptable if you do not know how to optimize memory.

 

 

 

Backpack II

Given n items with size Ai and value Vi, and a backpack with size m. What‘s the maximum value can you put into the backpack?

 Notice

You cannot divide item into small pieces and the total size of items you choose should smaller or equal to m.

Example

Given 4 items with size [2, 3, 5, 7] and value [1, 5, 2, 4], and a backpack with size 10. The maximum value is 9.

Challenge 

O(n x m) memory is acceptable, can you do it in O(m) memory?

 

[LintCode] BackPack Dynamic Programming Problems

标签:strong   log   tps   lint   lte   ram   ide   hal   alert   

原文地址:http://www.cnblogs.com/lz87/p/6928227.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!