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

  1. 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)

    [LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...

  2. Leetcode 300 Longest Increasing Subsequence

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  10. 【leetcode】300.Longest Increasing Subsequence

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

随机推荐

  1. vue.js随笔记---初识Vue.js

    1.基础要求: 1.1 HTML CSS JAVASCRIPT 1.2 模块化基础 1.3 Es6初步了解 2.vue.js 轻量级的MVVM模式框架,他同时吸收了recat和angular的优点,他 ...

  2. Java ssh 框架 hibernate 详细理解

    Hibernate框架技术相信对大多数的 java 程序员并不陌生,数据表之间的关系如何通过Hibernate来建立,需要我们认真的分析数据表中数据项之间的交互: 数据库表的之间的关系有: (1)一对 ...

  3. MySQL之使用DDL语句创建表

    一.使用DDL语句创建表 DDL语言全面数据定义语言(Data Define Language) 主要的DDL动词: CREATE(创建).DROP(删除).ALTER(修改) TRUNCATE(截断 ...

  4. Angular vs React 最全面深入对比

    如今,Angular和React这两个JavaScript框架可谓红的发紫,同时针对这两个框架的选择变成了当下最容易被问及或者被架构设计者考虑的问题,本文或许无法告诉你哪个框架更优秀,但尽量从更多的角 ...

  5. hdu4185二分图匹配

    Thanks to a certain "green" resources company, there is a new profitable industry of oil s ...

  6. Spring+SpringMvc+Mybatis 框架的搭建(一)

    本文是因为实习结束后学习到了新的技术,想写下来和更多人交流.开发中遇到的问题我也会一一说明,希望有更多人可以互相探讨,加入到一起来. 1. Spring+SpringMvc +Mybatis 的作用有 ...

  7. TypeScript基本知识(为学习AngularJS2框架做个小铺垫)

    学习angularjs2框架,需要了解一些TypeScript知识点,基本了解下面这几个知识点学习AngularJS2 就够用了 1.TypeScript 1.1显示类型的定义 TypeScript类 ...

  8. spring注解读取json文件

    开发时候在接口没有提供的时候,可以用json文件提前模拟接口数据 1.service层 package com.syp.spring.service; import java.io.File; imp ...

  9. PowerDesigner 12.5破解方法

    PowerDesigner 12.5破解方法 创建于 2017-05-07 22:18:04   一.下载   1 . PowerDesigner 12.5 官方下载地址  http://downlo ...

  10. Volley使用指南

    1.什么是Volley 在这之前,我们在程序中需要和网络通信的时候,大体使用的东西莫过于AsyncTaskLoader,HttpURLConnection,AsyncTask,HTTPClient(A ...