问题描述
我正在解决最长(严格)升序子序列问题,我必须在给定整数数组的情况下返回子序列的长度。我想出了这个递归:
int maxLength = 0;
int longestAscendingSubsequenceLength(int[] nums) {
longestAscendingSubsequenceLength(nums,null,-1,null);
return maxLength;
}
void longestAscendingSubsequenceLength(int[] nums,int lengthSoFar,Set<Integer> indexesToExclude,int bottomNumIndex,Integer bottomNum) {
for (int i=bottomNumIndex + 1; i < nums.length; i++) {
if (indexesToExclude!= null && indexesToExclude.contains(i)) {
continue;
}
int newBottomNum = nums[i];
if (indexesToExclude != null) {
indexesToExclude.add(i);
}
if ( (bottomNum == null) || (bottomNum < newBottomNum) ){
maxLength = Math.max(maxLength,lengthSoFar + 1);
longestAscendingSubsequenceLength(nums,lengthSoFar + 1,indexesToExclude,i,newBottomNum);
}
}
}
这有效,但几乎是一种蛮力方法,对于长时间的输入,它需要永远。我很难记住这个,因为每个调用都有多个改变的参数,所以如果我要使用 HashMap 作为备忘录,我不确定使用什么作为“键”,这样我就可以避免冗余计算。任何人都可以帮助或指出我正确的方向吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)