【Leetcode】Longest Increasing Subsequence

题目链接:https://leetcode.com/problems/longest-increasing-subsequence/

题目:

Given an unsorted array of integers, find the length of longest increasing subsequence.

For example,

Given [10, 9, 2, 5, 3, 7, 101, 18],

The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4.
Note that there may be more than one LIS combination, it is only necessary for you to return the length.

Your algorithm should run in O(n2) complexity.

Follow up: Could you improve it to O(n log n) time complexity?

思路:

1、简单动态规划,c[i]表示从0~i的数组中 包含nums[i]的LIS,状态转移方程:c[i]=max{c[j]+1} ,j<i且nums[i]>nums[j],时间复杂度为O(n^2)

2、动态规划加二分搜索,b[i]表示长度为i的LIS最后一个元素大小,end是b数组最后一个元素也就是当前LIS的下标。  对每个元素进行如下判断:

若nums[i]>b[end],则更新LIS,否则二分搜索b数组比nums[i]元素大的最小位置idx,此时b[idx]>nums[i]>b[idx-1] 更新idx位置,因为此时同样长度的子串,包含nums[i]的要比包含b[idx]要小。  时间复杂度O(nlogn)。

算法

1、

[java] view
plain
 copy

  1. public int lengthOfLIS(int[] nums) {
  2. if (nums.length == 0)
  3. return 0;
  4. int c[] = new int[nums.length];// c[i]表示从0~i 以nums[i]结尾的最长增长子串的长度
  5. c[0] = 1;
  6. int maxLength = 1;
  7. for (int i = 1; i < nums.length; i++) {
  8. int tmp = 1;
  9. for (int j = 0; j < i; j++) {
  10. if (nums[i] > nums[j]) {
  11. tmp = Math.max(c[j] + 1, tmp);
  12. }
  13. }
  14. c[i] = tmp;
  15. maxLength = Math.max(maxLength, c[i]);
  16. }
  17. return maxLength;
  18. }

2、

[java] view
plain
 copy

  1. public int lengthOfLIS(int[] nums) {
  2. if (nums.length == 0)
  3. return 0;
  4. int b[] = new int[nums.length + 1];// 长度为i的子串 最后一个数最小值
  5. int end = 1;
  6. b[end] = nums[0];
  7. for (int i = 1; i < nums.length; i++) {
  8. if (nums[i] > b[end]) {// 比最长子串最后元素还大,则更新最长子串长度
  9. end++;
  10. b[end] = nums[i];
  11. } else {// 否则更新b数组
  12. int idx = binarySearch(b, nums[i], end);
  13. b[idx] = nums[i];
  14. }
  15. }
  16. return end;
  17. }
  18. /**
  19. * 二分查找大于t的最小值,并返回其位置
  20. */
  21. public int binarySearch(int[] b, int target, int end) {
  22. int low = 1, high = end;
  23. while (low <= high) {
  24. int mid = (low + high) / 2;
  25. if (target > b[mid])
  26. low = mid + 1;
  27. else
  28. high = mid - 1;
  29. }
  30. return low;
  31. }
时间: 2024-10-10 07:37:08

【Leetcode】Longest Increasing Subsequence的相关文章

【Leetcode】Longest Increasing Path in a Matrix

题目链接:https://leetcode.com/problems/longest-increasing-path-in-a-matrix/ 题目: Given an integer matrix, find the length of the longest increasing path. From each cell, you can either move to four directions: left, right, up or down. You may NOT move dia

【leetcode】 Longest Substring Without Repeating Characters

题目: 给定一个字符串,返回该串没有重复字符的最长子串. 分析: 1)子串:子串要求是连续的. 2)无重复,出现重复就断了,必须从新的位置开始.而新的位置就是重复字符第一次出现位置的下一个位置. 3)整个串可能没有一处重复. 那么,为了找出当前访问的字符是否出现过,要怎么做呢?当然是hash,O(1)的时间,而且既然是字符, 定义个255的hash table 就可以了,hash table 中的元素为相应字符在s中出现的位置.初始化为-1,表示都没有出现. 我们另外定义一个start 和end

【LeetCode】Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run in

【LeetCode从零单刷】Longest Increasing Subsequence

题目: Given an unsorted array of integers, find the length of longest increasing subsequence. For example, Given [10, 9, 2, 5, 3, 7, 101, 18], The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more

LeetCode 300. Longest Increasing Subsequence

300. Longest Increasing Subsequence Description Submission Solutions Add to List Total Accepted: 64115 Total Submissions: 170859 Difficulty: Medium Contributors: Admin Given an unsorted array of integers, find the length of longest increasing subsequ

[LeetCode][JavaScript]Longest Increasing Subsequence

Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest increasing subsequence. For example,Given [10, 9, 2, 5, 3, 7, 101, 18],The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Not

leetcode 之 Longest Increasing Subsequence

题目描述: Given an unsorted array of integers, find the length of longest increasing subsequence. For example, Given [10, 9, 2, 5, 3, 7, 101, 18], The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be mor

[leetcode]300. Longest Increasing Subsequence最长递增子序列

Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Input: [10,9,2,5,3,7,101,18] Output: 4 Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4. Note: There may be more

LeetCode 300. Longest Increasing Subsequence最长上升子序列 (C++/Java)

题目: Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Input: [10,9,2,5,3,7,101,18] Output: 4 Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4. Note: There may be