Leetcode 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.
Your algorithm should run in O(n2) complexity.
Follow up: Could you improve it to O(n log n) time complexity?
用DP的方法,每一个dp[i]代表从nums[0]到这个元素nums[i]的最长的inreasing subsequence的长度。O(N^2)的解法:
class Solution(object):
def lengthOfLIS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums:
return 0
n = len(nums)
dp = [1] * n for i in range(0, n):
for j in range(i+1, n):
if nums[j] > nums[i]:
dp[j] = max(dp[i] + 1, dp[j]) return max(dp)
解法二 O(N logN)
本题可以用一个dp list来维护LIS的solution。对于nums里面的数跑一个for loop。如果遇到dp = [],需要把当前的num填到dp list里面。如果发现num大于dp list的最后(也就是最大的那个数)就append上去。如果num在[dp[0], dp[-1]]区间内,用binary search找到num应该在的位置去替换相应的数。这么做的目的是,例如上图中用5替换了7,假如5后面有6,7两个数的话,就会取得一个更长的subsequence。注意mid和num比较时使用的是>还是>=。这回区间头是移动到mid+1还是mid。
def lengthOfLIS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums:
return 0
size = len(nums)
dp = []
for x in range(size):
if not dp or dp[-1] < nums[x]:
dp.append(nums[x])
low, high = 0, len(dp) - 1
while low < high:
mid = (low + high)/2
if dp[mid] < nums[x]:
low = mid + 1
else:
high = mid
dp[high] = nums[x] return len(dp)
Leetcode 300 Longest Increasing Subsequence的更多相关文章
- [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最长递增子序列
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最长上升子序列 (C++/Java)
题目: Given an unsorted array of integers, find the length of longest increasing subsequence. Example: ...
- LeetCode——300. Longest Increasing Subsequence
一.题目链接:https://leetcode.com/problems/longest-increasing-subsequence/ 二.题目大意: 给定一个没有排序的数组,要求从该数组中找到一个 ...
- 【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. For example, ...
- 【刷题-LeetCode】300. Longest Increasing Subsequence
Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...
随机推荐
- Pycharm: keyboard reference
Source: Official set ♥ Editing Ctrl + Space Basic code completion (the name of any class, method or ...
- Basic: Fisher's transform
来源:http://bbs.chinahrd.net/thread-709742-1-1.html,Kenneth的回答. z = 0.5 * ln [ (1+r)/(1-r) ]" C0 ...
- elk-redis
yum install redis -y vim /etc/redis [root@linux-node1 etc]# grep '^[a-z]' /etc/redis.conf daemonize ...
- Linux socket多进程服务器框架三
在使用select管理服务器连接的时候: 注意1:select是可中断睡眠函数,需要屏蔽信号 注意2:必须获取select的返回值nread,每次处理完一个事件,nread需要-1 注意3:如果客户端 ...
- File类
存储在变量,数组和对象中的数据是暂时的,当程序终止时他们就会丢失.为了能够永久的保存程序中创建的数据,需要将他们存储到硬盘或光盘的文件中.这些文件可以移动,传送,亦可以被其他程序使用.由于数据存储在文 ...
- ios蓝牙开发(五)BabyBluetooth蓝牙库介绍
BabyBluetooth 是一个最简单易用的蓝牙库,基于CoreBluetooth的封装,并兼容ios和mac osx. 特色: 基于原生CoreBluetooth框架封装的轻量级的开源库,可以帮你 ...
- 走进 Spring IOC 的世界
转载出自:http://blog.csdn.net/m13666368773/article/details/7802126 1. IoC理论的背景我们都知道,在采用面向对象方法设计的软件系统中,它的 ...
- Theano2.1.9-基础知识之条件
来自:http://deeplearning.net/software/theano/tutorial/conditions.html conditions 一.IfElse vs Switch 这两 ...
- href的那些事
很多网站中都会使用<a>标签和 href属性来做链接,尤其在分页显示中用得最普遍.然而很多人对href的使用却并不十分了解. 1.href="#" 这个在网页中上滚回顶 ...
- 2015/11/9用Python写游戏,pygame入门(8):按钮和游戏结束
昨天没有更新内容,今天相对多写一些. 因为我们已经基本完成游戏框架,但是游戏结束后,并不知道怎样比较好开始.我本来本着懒的原则,想结束后显示一个黑屏,然后你重新点一下鼠标就重新开始.但是那样实在太不像 ...