LeetCode Longest Increasing Subsequence (LIS O(nlogn))

题意:

  给一个数组,求严格递增的最长递增子序列的长度。

思路:

  开销是一个额外的O(n)的数组。lower_bound(begin,end,val)的功能是:返回第一个大于等于val的地址。

 1 class Solution {
 2 public:
 3     int lengthOfLIS(vector<int>& nums) {
 4         if(nums.empty())    return 0;
 5         int *p=new int[nums.size()];
 6         p[0]=nums[0];
 7         int len=0;
 8         for(int i=1; i<nums.size(); i++)
 9         {
10             if(nums[i]>p[len])
11                 p[++len]=nums[i];
12             else
13                 *lower_bound(p,p+len+1,nums[i])=nums[i];
14         }
15         delete []p;
16         return ++len;
17     }
18 };

AC代码

时间: 11-14

LeetCode Longest Increasing Subsequence (LIS O(nlogn))的相关文章

POJ 2533 Longest Ordered Subsequence(LIS模版题)

Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 47465   Accepted: 21120 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ...

uva10405 - Longest Common Subsequence(LIS,最长共同自序列)

题目:uva10405 - Longest Common Subsequence(LIS,最长共同自序列) 题目大意:找出两个字符串中的最长公共的子序列. 解题思路:这类问题是第一次接触,不知道怎么做.百度了一下,发现了递推公式:dp[i][j]:代表第一个字符串的前i个字符和第二个字符串的前j个字符比较能得到的最长的公共子序列.s[i] == s[j] ,dp[i][j] = dp[i - 1][j - 1] + 1: s[i] != s[j] , dp[i][j] = Max (dp[i][

题解报告:poj 2533 Longest Ordered Subsequence(LIS)

Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence ( a1, a2, ..., aN) be any sequence ( ai1, ai2, ..., aiK), where 1 <= i1 < i2 < ... < iK <= N. For example, seq

POJ 2533 Longest Ordered Subsequence (LIS)

Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 35930   Accepted: 15779 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ...

[LeetCode] Longest Increasing Subsequence

A typical O(n^2) solution uses dynamic programming. Let's use lens[j] to denote the length of the LIS ending with nums[j]. The state equations are lens[0] = 1 lens[j] = max_{i = 0, 1, 2, ..., j - 1}(lens[j], lens[i] + 1) Then the length of the LIS of

Dynamic Programming | Set 3 (Longest Increasing Subsequence)

在 Dynamic Programming | Set 1 (Overlapping Subproblems Property) 和 Dynamic Programming | Set 2 (Optimal Substructure Property) 中我们已经讨论了重叠子问题和最优子结构性质,现在我们来看一个可以使用动态规划来解决的问题:最长上升子序列(Longest Increasing Subsequence(LIS)). 最长上升子序列问题,致力于在一个给定的序列中找到一个最长的子序列

DP(dynamic programming)之LIS(longest increasing subsequence)问题(转)

今天回顾WOJ1398,发现了这个当时没有理解透彻的算法.看了好久好久,现在终于想明白了.试着把它写下来,让自己更明白. 最长递增子序列,Longest Increasing Subsequence 下面我们简记为 LIS.排序+LCS算法 以及 DP算法就忽略了,这两个太容易理解了. 假设存在一个序列d[1..9] = 2 1 5 3 6 4 8 9 7,可以看出来它的LIS长度为5.下面一步一步试着找出它.我们定义一个序列B,然后令 i = 1 to 9 逐个考察这个序列.此外,我们用一个变

【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】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,