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的更多相关文章

  1. [LeetCode] 300. Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...

  2. leetcode@ [300] Longest Increasing Subsequence (记忆化搜索)

    https://leetcode.com/problems/longest-increasing-subsequence/ Given an unsorted array of integers, f ...

  3. [leetcode]300. Longest Increasing Subsequence最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...

  4. [leetcode] 300. Longest Increasing Subsequence (Medium)

    题意: 求最长增长的子序列的长度. 思路: 利用DP存取以i作为最大点的子序列长度. Runtime: 20 ms, faster than 35.21% of C++ online submissi ...

  5. LeetCode 300. Longest Increasing Subsequence最长上升子序列 (C++/Java)

    题目: Given an unsorted array of integers, find the length of longest increasing subsequence. Example: ...

  6. LeetCode——300. Longest Increasing Subsequence

    一.题目链接:https://leetcode.com/problems/longest-increasing-subsequence/ 二.题目大意: 给定一个没有排序的数组,要求从该数组中找到一个 ...

  7. 【LeetCode】300. Longest Increasing Subsequence 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 【leetcode】300.Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  9. 【刷题-LeetCode】300. Longest Increasing Subsequence

    Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...

随机推荐

  1. od破解实例

    百度经验: http://jingyan.baidu.com/article/636f38bb4091e4d6b84610a8.html pc6 http://www.pc6.com/edu/6278 ...

  2. Laterality issue on fMRI image

    The laterality issue: different software will interpret fMRI images in different way (mainly refer t ...

  3. MySQL Index详解

    FROM:http://blog.csdn.net/tianmo2010/article/details/7930482 ①MySQL Index 一.SHOW INDEX会返回以下字段 1.Tabl ...

  4. MathType 公式后的空格问题

    注册表编辑器修改 Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER\Software\Design Science\DSMT6\WordC ...

  5. 嵌入支付宝SDK,出现“LaunchServices: ERROR: There is no registered handler for URL scheme alipay”错误

    应用项目中嵌入支付宝SDK,在模拟器运行app后,会出现“LaunchServices: ERROR: There is no registered handler for URL scheme al ...

  6. 别出心裁的Linux系统调用学习法

    别出心裁的Linux系统调用学习法 操作系统与系统调用 操作系统(Operating System,简称OS)是计算机中最重要的系统软件,是这样的一组系统程序的集成:这些系统程序在用户对计算机的使用中 ...

  7. [OpenCVsharp]利用指针实现高速访问像素RGB值

    先简单介绍下什么是OpenCVsharp,内容取自百度百科 OpenCvSharp是一个OpenCV的.Net wrapper,应用最新的OpenCV库开发,使用习惯比EmguCV更接近原始的Open ...

  8. PhoneGap奇怪的现象:File FileTransfer download, 手机相册检测不到下载下来的图片(解决)

    我有个从服务器下载相片的功能在使用 File FileTransfer download api时,碰到了很奇怪的现象:图片已经从服务器里下载了,手机文件夹里也可以看到下载过来的图片,但是我的手机相册 ...

  9. JavaScript高级程序设计笔记 事件冒泡和事件捕获

    1.事件冒泡 要理解事件冒泡,就得先知道事件流.事件流描述的是从页面接收事件的顺序,比如如下的代码: <body> <div> click me! </div> & ...

  10. 《深入理解Spark:核心思想与源码分析》(前言及第1章)

    自己牺牲了7个月的周末和下班空闲时间,通过研究Spark源码和原理,总结整理的<深入理解Spark:核心思想与源码分析>一书现在已经正式出版上市,目前亚马逊.京东.当当.天猫等网站均有销售 ...