1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| package class072;
public class Code01_LongestIncreasingSubsequence { public static int lengthOfLIS2(int[] nums) { int n = nums.length; int[] ends = new int[n]; int len = 0; for (int i = 0, find; i < n; i++) { find = bs1(ends, len, nums[i]); if (find == -1) { ends[len++] = nums[i]; } else { ends[find] = nums[i]; } } return len; } public static int bs1(int[] ends, int len, int num) { int l = 0, r = len - 1, m, ans = -1; while (l <= r) { m = (l + r) / 2; if (ends[m] >= num) { ans = m; r = m - 1; } else { l = m + 1; } } return ans; }
public static int bs2(int[] ends, int len, int num) { int l = 0, r = len - 1, m, ans = -1; while (l <= r) { m = (l + r) / 2; if (ends[m] > num) { ans = m; r = m - 1; } else { l = m + 1; } } return ans; }
}
|