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 ...
随机推荐
- 移动开发webapp开发常用meta设置手机浏览器全屏模式
1.WebApp全屏模式: <meta name="viewport" content="width=device-width,initial-scale=1.0, ...
- zlog学习笔记(zc_arraylist)
zc_arraylist.h /** * 实现类似列表的功能 * */ #ifndef __zc_arraylist_h #define __zc_arraylist_h #define ARRAY_ ...
- java.sql.preparedstatement和java.sql.statement的区别
本文转自CSDN,然后整理了一遍.原文出处:CSDN JDBC(java database connectivity,java数据库连接)的api中的主要的四个类之一的java.sql.stateme ...
- Win10添加简体中文美式键盘的方法
在Win10中很多朋友发现没有简体中文(美式键盘)的选项,而如果使用“英语-美式键盘”作为默认输入法,有ModernApp的界面会变成英文,这十分不方便,那么有没有方可以在Win10中添加一个 简体中 ...
- JavaScript继承与原型链
对于那些熟悉基于类的面向对象语言(Java 或者 C++)的开发者来说,JavaScript 的语法是比较怪异的,这是由于 JavaScript 是一门动态语言,而且它没有类的概念( ES6 新增了c ...
- 在Azure上搭建Orchard CRM入口网站
这是英文版:Setup Orchard CRM portal website on Azure
- IBM WebSphere MQ 7.5基本用法
一.下载7.5 Trial版本 http://www.ibm.com/developerworks/downloads/ws/wmq/ 这是下载网址,下载前先必须注册IBM ID,下载完成后一路Nex ...
- Android -- ActivityLifeCycleCallbacks
ActivityLifeCycleCallbacks Application通过此接口提供了一套回调方法,用于让开发者对Activity的生命周期事件进行集中处理. 为什么用ActivityLifec ...
- 微软虚拟学院MVA 字幕获取方法
微软虚拟学院(MVA)上有一些不错的视频教程,但是,蛋疼的一点那就是视频要不就慢,要不就卡,总之当你的思维跟着视频深入的时候,duang~,卡一下,说不定就要重头开始,所幸的是提供了视频下载,下载速度 ...
- Incorrect string value异常解决
mysql数据库的一个问题 1366-Incorrect string value:'\xE5\x8D\xA1\xE5......' for column 'filename' at row 1 问题 ...