300. Longest Increasing Subsequence
来源:互联网 发布:网络推广微商seocnm 编辑:程序博客网 时间:2024/06/05 07:40
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?
Credits:
Special thanks to @pbrother for adding this problem and creating all test cases.
class Solution {public: // There's a typical DP solution with O(N^2) Time and O(N) space // DP[i] means the result ends at i // So for dp[i], dp[i] is max(dp[j]+1), for all j < i and nums[j] < nums[i] int lengthOfLIS(vector<int>& nums) { const int size = nums.size(); if (size == 0) { return 0; } vector<int> dp(size, 1); int res = 1; for (int i = 1; i < size; ++i) { for (int j = 0; j < i; ++j) { if (nums[j] < nums[i]) { dp[i] = max(dp[i], dp[j]+1); } } res = max (res, dp[i]); } return res; }};第二种是复杂度O(nlogn)的二分查找方法。建立数组存放可能存在的升序子序列。从头遍历nums,试图把每一个num放到dp相应的位置里,最后返回dp存下的最长升序自序列的长度。代码如下:
public class Solution { public int lengthOfLIS(int[] nums) { int[] dp = new int[nums.length]; int size = 0; for (int x: nums) { int i = -1, j = size; while (i + 1 < j) { int mid = i + (j - i) / 2; if (dp[mid] < x) { i = mid; } else { j = mid; } } if (i < 0) { dp[0] = x; } else { dp[i + 1] = x; } if (i + 1 == size) { dp[size] = x; size ++; } } return size; }}
0 0
- 300. Longest Increasing Subsequence
- 300. Longest Increasing Subsequence
- 300. Longest Increasing Subsequence
- 300. Longest Increasing Subsequence
- 300. Longest Increasing Subsequence
- 300. Longest Increasing Subsequence
- 300. Longest Increasing Subsequence
- 300. Longest Increasing Subsequence
- 300. Longest Increasing Subsequence
- 300. Longest Increasing Subsequence
- 300. Longest Increasing Subsequence
- 300. Longest Increasing Subsequence
- 300. Longest Increasing Subsequence
- 300. Longest Increasing Subsequence
- 300. Longest Increasing Subsequence
- 300. Longest Increasing Subsequence
- 300. Longest Increasing Subsequence
- 300. Longest Increasing Subsequence
- haproxy+keepalived实现高可用负载均衡
- 预防SQL注入攻击之我见
- ASR插座研发进度及功能概述
- ping: Network is unreachable
- AJAX
- 300. Longest Increasing Subsequence
- CSS学习笔记
- 兼容性管理
- 电脑入门知识
- JDBC连接数据库的步骤
- 兼容之ajax
- 2017年开始今年开始iOS 10设备上的app必须通过https且是TLS 1.2的协议去访问web资源
- WordPress 4.7.4 中文正式版发布
- 函数、闭包及匿名函数(Lambda)