[Leetcode] Binary search, DP--300. 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 than one LIS combination, it is only necessary for you to return the length.
Solution:
#1. naive method; time complexity o(n^2). two layers iteration. 1) first iterate each element nums[i] in the nums, , 2) second iteration to find the bigger num[j] to add as each list 3) then find the maximum length list in the lists. length[i] += 1 if nums[j] > nums[i]
#or reversely, smaller nums[j] to update length[i]. i to 0 to len(nums), j = 0 to i; so length[i] = max(length[i], length[j]+1)
#2n d use binary search, try to select and insert into the increasing sequence
#(1) maintain a result list ans = [nums[0]]
#(2) iterate nums from second element num, compare num with the last element of ans:
# a. if num < ans[-1]
# insert num into ans
# else binary search in the ans the left insertion position for num (i.e. the smallest number that is bigger than num), and replace it
def binarySearch(lst, ele):
if len(lst) == 1:
return 0
l = 0
h = len(lst) - 1
while (l <= h):
mid = (l+h)/2
if lst[mid] == ele:
return mid
elif lst[mid] < ele:
l = mid + 1
else:
h = mid - 1
if l >= len(lst):
return -1
return l if len(nums) == 0:
return 0
ansLst = []
ansLst.append(nums[0])
for i in range(1, len(nums)):
if nums[i] > ansLst[-1]:
ansLst.append(nums[i])
else:
#binary search
pos = binarySearch(ansLst, nums[i])
#print ('pos: ', len(ansLst), pos)
ansLst[pos]
return len(ansLst)
#note it is for length of longest increasing sequence, the final ansLst may not be the real longest increasing sequence
#3rd use Dynamic programming
#use DP[i] indicate the length of longest increasing sequence at position i so far.
#it has optimal substructure: every sublist has the optimal solution for the longest increasing sequence
#overlapping subproblem: the large sublist problem is affected by the previous smaller sublist :
#the transition equation: DP[i] = max(DP[i], DP[j] + 1) ; i = 1 to len(nums), j = 0 to i
#intialize all DP element as 1
if len(nums) == 0:
return 0 dp = [1] * len(nums)
for i in range(1, len(nums)):
for j in range(0, i):
if nums[j] < nums[i]:
dp[i] = max(dp[i], dp[j] + 1)
return max(dp)
[Leetcode] Binary search, DP--300. Longest Increasing Subsequence的更多相关文章
- 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
- Leetcode 300 Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- LeetCode 300. Longest Increasing Subsequence最长上升子序列 (C++/Java)
题目: Given an unsorted array of integers, find the length of longest increasing subsequence. Example: ...
- [LeetCode] 300. Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- leetcode@ [300] Longest Increasing Subsequence (记忆化搜索)
https://leetcode.com/problems/longest-increasing-subsequence/ Given an unsorted array of integers, f ...
- 【LeetCode】300. Longest Increasing Subsequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [leetcode]300. Longest Increasing Subsequence最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- [leetcode] 300. Longest Increasing Subsequence (Medium)
题意: 求最长增长的子序列的长度. 思路: 利用DP存取以i作为最大点的子序列长度. Runtime: 20 ms, faster than 35.21% of C++ online submissi ...
- 【刷题-LeetCode】300. Longest Increasing Subsequence
Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...
- 【leetcode】300.Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
随机推荐
- Docker for Mac与IntelliJ Docker Integration插件的兼容性问题
笔者在自己的Mac上安装的是Docker for Mac,而不是Docker Toolbox. 这两者最主要的区别在于Docker for Mac用HyperKit作为虚拟化解决方案而不是Virtua ...
- oracle 的 SDO_GEOMETRY
元数据定义 CREATE OR REPLACE TYPE MDSYS.SDO_GEOMETRY AS OBJECT ( SDO_GTYPE NUMBER, SDO_SRID NUMBER, SDO_P ...
- LINQ TO XML初步了解
最近简单的学习了一下LINT TO XML,写篇博客在这,方便以后查看~~ 1.常用到的类 XmlDocument -- 文档(xml文件) XmlElement -- ...
- 百度开源上传组件WebUploader的formData动态传值技巧
基于Web页面的文件上传一直是互联网应用开发中避免不了的,从asp时代的AspUpload组件.到asp无组件上传,到.Net时代的FileUpload,再到HTML5时代的各种基于jQuery的上传 ...
- javascript的null 和undifined
null表示一个对象,或者变量的值为空,undifined 表示声明了一个对象(变量),没有给他初始化或者压根儿就没有声明这个对象(变量). 1.null 是JavaScript关键字 undifin ...
- web前端技术框架选型参考
一.出发点 随着Web技术的不断发展,前端架构框架.UI框架.构建工具.CSS预处理等层出不穷,各有千秋.太多的框架在形成初期,都曾在web领域 掀起过一场技术浪潮,可有些却仅仅是昙花一现,随着他们用 ...
- Yomob广告在cocos2dx安卓平台的Demo
Yomob 也是一个广告平台:他的使用方式比较简单哈,这里只说说他的步骤 按照官方文档说明配置环境,我使用的是eclipse,在eclipse上自己将文件copy到项目proj.android下,覆盖 ...
- UML总结(对九种图的认识和如何使用Rational Rose 画图)
UML是一种建模语言,是系统建模的标准.我们之所以建模是因为大规模的系统设计时相当复杂的,当系统比较复杂时就会涉及到以下这几个问题: 开发人员如何与用户进行沟通来了解系统的需求? 开发人员之间如何沟通 ...
- 初识Celery
本系列文章的开发环境: window + python2. + pycharm5 + celery3.1.25 + django1.9.4 在我们日常的开发工作中,经常会遇到这几种情况: 1.在web ...
- 关于使用JavaScript实现图片点击切换(附带改变导航图片 方案二)
具体需求见方案一. 这种方案相比方案一更加专业. <!DOCTYPE html> <html> <head lang="en"> <met ...