Leetcode-300. Longest Increasing Subsequence

网友投稿 901 2022-08-25

Leetcode-300. Longest Increasing Subsequence

Leetcode-300. Longest Increasing Subsequence

​​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 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?

题解:dag动态规划模型,O(nlogn)没想到,看讨论是用二分搜索。

class Solution {public: int lengthOfLIS(vector& nums) { int l = nums.size(); if (l <= 1){ return l; } int ans = 1; vector dp(l, 1); for (int i = 1; i < l; i++){ for (int j = 0; j < i; j++){ if (nums[j] < nums[i]){ dp[i] = max(dp[i], dp[j] + 1); } } ans = max(ans, dp[i]); } return ans; }};

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:LeetCode-861. Score After Flipping Matrix
下一篇:LeetCode-1262. Greatest Sum Divisible by Three
相关文章

 发表评论

暂时没有评论,来抢沙发吧~