【发布时间】:2017-04-26 17:31:10
【问题描述】:
我有以下问题:
找到给定序列/数组的最长递增子序列。
换句话说,找到数组的一个子序列,其中子序列的 元素严格按递增顺序排列,其中 子序列尽可能长。这个子序列不是 必然是连续的,或唯一的。在这种情况下,我们只关心 最长递增子序列的长度。
例子:
输入:[0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15] 输出 : 6 序列:[0, 2, 6, 9, 13, 15] 或 [0, 4, 6, 9, 11, 15] 或 [0, 4、6、9、13、15]
这是一个 DP 问题,我在记忆步骤中确实有一些问题。 这是我的代码:
public int lis(final List<Integer> a) {
return maxIncreasing(0, Integer.MIN_VALUE, a);
}
HashMap<Integer, Integer> memo = new HashMap<>();
private int maxIncreasing(int index, int lastElt, final List<Integer> a) {
if(memo.containsKey(index)) return memo.get(index);
// end?
if(index >= a.size()) return 0;
int weTake = Integer.MIN_VALUE;
// can we take it?
if(a.get(index) > lastElt) {
// take it or don't
weTake = maxIncreasing(index + 1, a.get(index), a) + 1;
}
int weDoNot = maxIncreasing(index + 1, lastElt, a);
int max = Math.max(weTake, weDoNot);
memo.put(index, max);
return max;
}
如果没有备忘录 HashMap,我得到了正确的结果,我不知道为什么一旦到位就会给我错误的结果。
谢谢。
【问题讨论】:
标签: java algorithm dynamic-programming