博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode-Longest Increasing Subsequence
阅读量:4611 次
发布时间:2019-06-09

本文共 1396 字,大约阅读时间需要 4 分钟。

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?

Analysis:

https://segmentfault.com/a/1190000003819886

tails[i]: for an increasing subsequence with length i, what is the tail element in nums[].

 

Solution:

1 public class Solution { 2     public int lengthOfLIS(int[] nums) { 3         if (nums.length==0) return 0; 4          5         int[] tails = new int[nums.length]; 6         tails[0] = 0; 7          8         int res = 0; 9         for (int i=1;i
res) res = pos;14 }15 return res+1;16 }17 18 public int findPos(int[] nums, int[] tails, int val, int end){19 int start = 0;20 while (start<=end){21 int mid = start + (end-start)/2;22 if (val == nums[tails[mid]]) return -1;23 else if (val < nums[tails[mid]]){24 end = mid-1;25 } else {26 start = mid+1;27 }28 }29 return start;30 }31 }

 

转载于:https://www.cnblogs.com/lishiblog/p/5724676.html

你可能感兴趣的文章
centos7+nginx 1.9.0+php-fpm+phpstorm+xdebug+vmware开发环境搭建
查看>>
"下列引导或系统启动驱动程序无法加载: cdrom"的解决方案
查看>>
数据库设计
查看>>
面试系列11 缓存是如何使用
查看>>
ActiveX插件的Z-Index属性无效问题解决
查看>>
extjs中的JS代码在firefox可以正常运行,在IE中无法运行的方法。
查看>>
vs2010编辑器中代码前的虚线问题
查看>>
loadrunner:web services接口测试
查看>>
有关implicit Intent的使用
查看>>
关于Android log拿不到的情况
查看>>
spring入门——applicationContext与BeanFactory的区别
查看>>
微服务架构
查看>>
在WPF中获取DataGridTemplateColumn模板定义的内容控件
查看>>
WPF疑难杂症之二(全屏幕窗口)
查看>>
Directx11教程(9) 增加一个TimerClass类
查看>>
Directx11教程(45) alpha blend(2)
查看>>
C# WPF 滚动字幕实现
查看>>
win10 uwp 毛玻璃
查看>>
[ 搭建Redis本地服务器实践系列一 ] :图解CentOS7安装Redis
查看>>
Wix 安装部署教程(七) 获取管理员权限
查看>>