求Longest_Increasing_Subsequence JAVA O(nlgn) 代码# JobHunting - 待字闺中m*k2012-11-08 08:111 楼只找到C++版的http://www.algorithmist.com/index.php/Longest_Increasing_Subseq
m*g2012-11-08 08:112 楼public static int longestIncreasingSubConsequence(int[] array) {int length = array.length;int[] valueAtLength = new int[length];int maxNow = 1;valueAtLength[0] = array[0];for(int i = 1; i < length; i++) {int updatePos = findPos(valueAtLength, maxNow, array[i]);if(updatePos == maxNow) { valueAtLength[maxNow++] = array[i];} else {valueAtLength[updatePos] = array[i];}}return maxNow;}private static int findPos(int[] array, int length, int target) {int low = 0;int high = length - 1;while(low <= high) {int mid = (low + high) / 2;if(mid == low) {if(array[mid] >= target) {return mid;} else {mid = mid + 1;if(mid > high) {break;}}}if(array[mid] >= target && array[mid - 1] < target) {return mid;} else if (array[mid] < target) {low = mid + 1;} else if(array[mid - 1] >= target) {high = mid - 1;}}return length;}变量的naming不好,自己可以修改
m*k2012-11-08 08:113 楼谢谢myanything 大虾,int[] array= new int[] { 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7,15};如何输出 0 2 6 9 11 15 呢?
m*k2012-11-08 08:115 楼刚在collabedit上被问到这道题,干脆就直接拷贝了,面试官,一国人兄弟说他看到过这段code,既然这样,我也不需隐瞒,直接告诉他就是我发帖要的code,再装没见过就太掩耳盗铃了,呵呵。看来国人兄弟也是有意放水,谢过了哈!