摘要: Give a dynamic-programming solution to the 0-1 knapsack problem that runs inO(nW) time, where n is the number of items and W is the maximum weight ofi... 阅读全文
posted @ 2014-06-10 12:17 门对夕阳 阅读(356) 评论(0) 推荐(0) 编辑
摘要: 给一个长度为 n 的杆子,切成小段卖出去,价格根据小段的长度不同而不同。下面是一个例子我们要通过切成小段卖出尽可能高的总价钱。问题是:How to decompose the problem?Decomposition 的第一步是:第一刀切在哪?可以切在最左边(等于整根卖出去);可以切在位置1,位置... 阅读全文
posted @ 2014-06-10 12:13 门对夕阳 阅读(391) 评论(0) 推荐(0) 编辑
摘要: 所有的 DP 问题都可以简单得用 Recursion 的方式实现。这通常是最容易想到的思路。问题是这种实现不够 efficient,存在 subproblem 被重复计算的情况。有两种解决这个问题的方法:1. 很直观的,在 naive recursion 里加入 一个 save 的环境,把每个 su... 阅读全文
posted @ 2014-06-10 11:06 门对夕阳 阅读(288) 评论(0) 推荐(0) 编辑
摘要: Write an algorithm to print all ways of arranging eight queens on an 8*8 chess board so that none of them share the same row, column or diagonal.思路:本质... 阅读全文
posted @ 2014-06-10 03:11 门对夕阳 阅读(233) 评论(0) 推荐(0) 编辑