摘要: 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) 编辑
摘要: Given an infinite number of quarters (25 cents), dimes (10 cents), nickels (5 cents) and pennies (1 cent), find how many ways to represent n cents.思路:... 阅读全文
posted @ 2014-06-09 05:11 门对夕阳 阅读(479) 评论(0) 推荐(0) 编辑
摘要: Implement an algorithm to print all valid ( properly opened and closed) combinations of n-pairs of parentheses.思路:比如 n = 3, ((())) 就是一个valid combinati... 阅读全文
posted @ 2014-06-06 13:30 门对夕阳 阅读(259) 评论(0) 推荐(0) 编辑
摘要: Problem: Compute all permutations of a string of unique characters.此题用循环的方式不好做,下面是一种递归的思路:把给的字符串看成一个字符集合,每次从这个集合里拿出一个字符来填到下面的空格中。填的顺序从左到右。把a1填到第一个空格里是... 阅读全文
posted @ 2014-06-06 09:34 门对夕阳 阅读(367) 评论(0) 推荐(0) 编辑
摘要: 更新到新的 Mac OS X 再打开Eclipse 编译程序会报错,Exception in thread "main" java.lang.UnsupportedClassVersionError: Unsupported major.minor version 51.0原因是Eclipse 找不... 阅读全文
posted @ 2014-06-06 03:23 门对夕阳 阅读(2347) 评论(0) 推荐(0) 编辑
摘要: java.util.ArraysThis class deals with 'real' arrays in java, in the form of T[]. Thus it doesn't deal with ArrayList, java.util.Collections deals with... 阅读全文
posted @ 2014-06-05 07:21 门对夕阳 阅读(116) 评论(0) 推荐(0) 编辑
摘要: 其一,所有的递归实现都可以 iteratively 实现,虽然很多时候递归的代码更简洁。其二,递归会产生多余的开销,包括空间和时间。其三,如果一定要递归,不要在一个递归函数里做多件事,最好只做一件。像下面的代码(未完成,因为写不下去了),试图在递归函数里做三件事,(1) 检测路径是否存在(2) 构造... 阅读全文
posted @ 2014-06-01 06:29 门对夕阳 阅读(380) 评论(0) 推荐(0) 编辑