Leetcode 300. Longest Increasing Subsequence
来源:互联网 发布:网络谣言的刑法规制 编辑:程序博客网 时间:2024/06/15 15:28
问题描述
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.
O(n2)解法:
定义数组dp[i]表示前i个数的最长递增序列的长度,则状态转移矩阵:
dp[i]=max(dp[j])+1,(j
public int lengthOfLIS(int[] nums) { if(nums==null) return 0; int n=nums.length; if(n==0) return 0; int[]dp=new int[n]; dp[0]=1; int ans=1; for(int i=1;i<n;i++){ for(int j=0;j<i;j++){ if(nums[i]>nums[j]&&dp[i]<dp[j]) dp[i]=dp[j]; } dp[i]++; if(ans<dp[i]) ans=dp[i]; } return ans; }
O(nlogn)解法:二分查找
维护一个递增的序列,对其进行二分查找当前元素nums[i],覆盖原有元素或者在末尾添加。
代码:
public int lengthOfLIS(int[] nums) { if(nums==null) return 0; int n=nums.length; if(n==0) return 0; int[]dp=new int[n]; dp[0]=nums[0]; int len=1; for(int i=1;i<n;i++){ int low=0,high=len-1; while(low<=high){ int mid=low+(high-low)/2; if(nums[i]<=dp[mid]) high=mid-1; else low=mid+1; } if(low>=len) dp[len++]=nums[i]; else dp[low]=nums[i]; } return len; }
阅读全文
0 0
- Leetcode-300.Longest Increasing Subsequence
- 300. Longest Increasing Subsequence LeetCode
- [leetcode] 300. Longest Increasing Subsequence
- leetcode 300. Longest Increasing Subsequence
- LeetCode *** 300. Longest Increasing Subsequence
- LeetCode 300. Longest Increasing Subsequence
- LeetCode-300.Longest Increasing Subsequence
- [leetcode] 300. Longest Increasing Subsequence
- leetcode 300. Longest Increasing Subsequence
- LeetCode 300. Longest Increasing Subsequence
- leetcode 300. Longest Increasing Subsequence
- [LeetCode]--300. Longest Increasing Subsequence
- [leetcode] 300. Longest Increasing Subsequence
- LeetCode 300. Longest Increasing Subsequence
- 【LeetCode】300. Longest Increasing Subsequence
- 【LeetCode】300. Longest Increasing Subsequence
- Leetcode 300. Longest Increasing Subsequence
- 【LeetCode】300. Longest Increasing Subsequence
- 应用anaconda一键安装python及配置numpy scipy theano,pylearn2各种包
- 解决使用jrebel,集成在eclipse的tomcat里面,修改类和jsp都可以加载修改后的类和文件,无法马上更新修改过的jsp
- iOS git checkout之一 HEAD基本和detached 状态
- STL常用函数复习之————bitset
- win10下 maven 错误解决。
- Leetcode 300. Longest Increasing Subsequence
- 关于SIGPIPE信号
- 题解
- Codeforces Round #422 (Div. 2) B. Crossword solving
- FPGA学习记录二
- map的应用 热血格斗场
- 维特比算法viterbi的简单实现 python版
- ubuntu16.04安装PHP7.0的laravel必须扩展
- rsync命令----解决远程拷贝一个问题通过scp不能复制ln文件