1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > LeetCode-最长递增子序列和最大的递增子序列

LeetCode-最长递增子序列和最大的递增子序列

时间:2019-02-21 03:13:28

相关推荐

LeetCode-最长递增子序列和最大的递增子序列

1.求最长递增子序列长度

方法一:动态规划O(n2)

dp[i]:以i结尾的最长递增子序列

初始化:dp[*]=1

公式:dp[i]=max(dp[j]+1) and nums[i] > nums[j],0<=j<i

结果:max(dp)

public static int findLongest2(int[] A) {int n = A.length;int[] f = new int[n];// 用于存放f(i)值;f[0] = 1;// 以第a1为末元素的最长递增子序列长度为1;int maxLen = Integer.MIN_VALUE;for (int i = 1; i < n; i++)// 循环n-1次{f[i] = 1;// f[i]的最小值为1;for (int j = 0; j < i; j++)// 循环i 次{if (A[j] < A[i] && f[j] +1 > f[i]) {f[i] = f[j] + 1;// 更新f[i]的值。maxLen = Math.max(maxLen, f[i]);}}}return maxLen;}

方法二:O(nlogn)

思路:

例子:假设存在一个序列d[1..9] = 2 1 5 3 6 4 8 9 7,可以看出来它的LIS长度为5。

定义一个序列B,与的d同大小。

此外,我们用一个变量Len来记录现在最长算到多少了

首先,把d[1]有序地放到B里,令B[1] = 2,就是说当只有1一个数字2的时候,长度为1的LIS的最小末尾是2。这时Len=1

然后,把d[2]有序地放到B里,令B[1] = 1,就是说长度为1的LIS的最小末尾是1,d[1]=2已经没用了,很容易理解吧。这时Len=1

接着,d[3] = 5,d[3]>B[1],d[3]=5,就是说长度为2的LIS的最小末尾是5,很容易理解吧。这时候B[1..2] = 1, 5,Len=2

再来,d[4] = 3,它正好加在1,5之间,放在1的位置显然不合适,因为1小于3,长度为1的LIS最小末尾应该是1,这样很容易推知,长度为2的LIS最小末尾是3,于是可以把5淘汰掉,这时候B[1..2] = 1, 3,Len = 2

继续,d[5] = 6,它在3后面,因为B[2] = 3, 而6在3后面,于是很容易可以推知B[3] = 6, 这时B[1..3] = 1, 3, 6,还是很容易理解吧? Len = 3 了。

第6个, d[6] = 4,你看它在3和6之间,于是我们就可以把6替换掉,得到B[3] = 4。B[1..3] = 1, 3, 4, Len继续等于3

第7个, d[7] = 8,它很大,比4大,嗯。于是B[4] = 8。Len变成4了

第8个, d[8] = 9,得到B[5] = 9,嗯。Len继续增大,到5了。

最后一个, d[9] = 7,它在B[3] = 4和B[4] = 8之间,所以我们知道,最新的B[4] =7,B[1..5] = 1, 3, 4, 7, 9,Len = 5。

java代码如下:

public static int findLongest(int[] A) {int n = A.length;int[] B = new int[n + 1];B[1] = A[0];int len = 1,index;for (int i = 1; i < n; i++) {if (A[i] > B[len]) {len++;B[len] = A[i];} else {index = search(1, len, B, A[i]);B[index] = A[i];}}return len;}

// 二分查找,查找第一个比他大的数public static int search(int start, int end, int[] a, int target) {int mid;while (start < end) {mid = (start + end) / 2;if (target >= a[mid])start = mid + 1;elseend = mid;}return start;}

python代码比较简单,如下:

from bisect import bisect_leftdef lengthOfLIS(nums: List[int]) -> int:dp = []for x in nums:if not dp or x > dp[-1]:dp.append(x)else:dp[bisect_left(dp, x)] = xreturn len(dp)

2.打印最长递增子序列

方法一:打印第一个最长子序列O(n2)

输入例子:

7

89 256 78 1 46 78 8

// 打印第一个最长序列public static ArrayList<Integer> maxSubIncreaseArray(int[] array) {int n = array.length;int[] list = new int[n];//存储每个数结尾的最长串ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();// 存储所有递增序列ArrayList<Integer> tmp = new ArrayList<Integer>();int index = -1;// 用于标记当前元素之前的第一个递增子序列的位置int maxIndex = 0;// 用于标记该序列的最长递增子序列的位置int max = Integer.MIN_VALUE;// 最长递增子序列的长度list[0] = 1;// 该列表用于标记包括当前元素在内的前半部分的最长递增子序列的长度tmp.add(array[0]);res.add(tmp);for (int i = 1; i < n; i++) {index = -1;tmp = new ArrayList<Integer>();for (int j = 0; j < i; j++) {if (array[j] < array[i] && list[j]+1 > list[i]) {list[i] = list[j]+1;index = j;}}if (index > -1)tmp.addAll(res.get(index));tmp.add(array[i]);res.add(tmp);if (list[i] > max) {max = list[i];maxIndex = i;}}return res.get(maxIndex);}

方法二:打印最后一个最长字符串O(nlogn),在求长度的方法上,与上述方法相结合

public static int findLongest(int[] A) {int n = A.length;int[] B = new int[n + 1];int[] C = new int[n + 1];//存储i长的字符串位置C[1] = 1;B[1] = A[0];int len = 1, mid;ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();// 存储所有递增序列ArrayList<Integer> tmp = new ArrayList<Integer>();int index = -1;// 用于标记当前元素之前的第一个递增子序列的位置int maxIndex = 1;// 用于标记该序列的最长递增子序列的位置int max = Integer.MIN_VALUE;// 最长递增子序列的长度tmp.add(A[0]);res.add(new ArrayList<Integer>());res.add(tmp);for (int i = 1; i < n; i++) {tmp = new ArrayList<Integer>();if (A[i] > B[len]) {len++;B[len] = A[i];C[len] = i + 1;tmp.addAll(res.get(C[len - 1]));tmp.add(A[i]);if (len > max) {max = len;maxIndex = i + 1;}} else {index = search(1, len, B, A[i]);B[index] = A[i];while (index > 0 && B[index] >= A[i]) {index--;}if (index > 0) {tmp.addAll(res.get(C[index]));C[index + 1] = res.size();} else {C[1] = res.size();}tmp.add(A[i]);}res.add(tmp);}// 打印最后一个最长序列System.out.println(res.get(maxIndex));return len;}

3.和最大的递增子序列的和

// 和最大的递增子序列public static long SubLongestAscendSum(int[] a) {int n = a.length;int f[] = new int[n];f[0] = a[0];long max = f[0];for (int i = 1; i < a.length; i++) {f[i] = a[i];for (int j = 0; j < i; j++) {if (a[j] < a[i] && f[j] + a[i] > f[i]) {f[i] = f[j] + a[i];max = Math.max(f[i], max);}}}return max;}

4. 打印和最大的递增子序列的和

// 打印和最大的递增子序列public static List<Integer> SubLongestAscendSum2(int[] a) {int n = a.length;int f[] = new int[n];f[0] = a[0];long max = f[0];ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();// 存储所有递增序列ArrayList<Integer> tmp = new ArrayList<Integer>();int index = -1;// 用于标记当前元素之前的第一个递增子序列的位置int maxIndex = 0;// 用于标记该序列的最长递增子序列的位置tmp.add(a[0]);res.add(tmp);for (int i = 1; i < a.length; i++) {f[i] = a[i];index = -1;tmp = new ArrayList<Integer>();for (int j = 0; j < i; j++) {if (a[j] < a[i] && f[j] + a[i] > f[i]) {f[i] = f[j] + a[i];index = j;}}if (index > -1)tmp.addAll(res.get(index));tmp.add(a[i]);res.add(tmp);if (max < f[i]) {max = f[i];maxIndex = i;}}return res.get(maxIndex);}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。