[LintCode] BackPack Dynamic Programming Problems

Posted Push your limit!

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LintCode] BackPack Dynamic Programming Problems相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

背包型动态规划

92 背包问题

Backpack | & ||

设置中的 Laravel-Backpack/Settings 表字段

在 MyAccountController [backpack-laravel] 中添加个人资料信息

Ruby绘本时间《The Backpack》