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. STM32F0的flash读写

    flash大小64k Rom+8k Ram的大小,stm32f051有64k Rom,总的分为 64页,一页1024byte ,在flash的Rom里面写数据掉电保存,相当于W25q80 uint32 ...

  2. jvm的搭建

    首先先 说明一下接下来要用到的,环境变量中的path和classpath的区别 1.path路径用来告诉计算机.exe文件的路径,classpath路径是用来告诉计算机.class文件的路径 2.系统 ...

  3. Linux密码保护

    在之前写了Linux密码破解的方法,虽然这样对于忘记密码时很方便,但同时别人也可以很轻易的破解你的Liunx虚拟机,安全问题存在隐患. 下面给出一些Liunx密码的安全防护操作: 1.防止破解root ...

  4. Docker 组件如何协作?- 每天5分钟玩转容器技术(8)

    还记得我们运行的第一个容器吗?现在通过它来体会一下 Docker 各个组件是如何协作的. 容器启动过程如下: Docker 客户端执行 docker run 命令. Docker daemon 发现本 ...

  5. Javascript一道面试题

    实现一个函数,运算结果可以满足如下预期结果: add(1)(2) // 3add(1, 2, 3)(10) // 16 add(1)(2)(3)(4)(5) // 15 function add () ...

  6. sql关键字之null

    在数据库中使用一种特殊的值表示未知的值--NULL,我们称之为空值但并不是空的字符串,而是特殊的值.

  7. jQuery的hover方法搭配css的hover选择器,实现选中元素突出显示

    问题简述: 今天做帮一个师姐做网页遇到一个这样的要求: 鼠标不移动进表格,表格透明度不变. 鼠标移动进表格,hover到的单元格透明度不变,没hover到的单元格透明度改变. 先贴我已经实现好的效果, ...

  8. 【Netty】第一个Netty应用

    一.前言 前面已经学习完了Java NIO的内容,接着来学习Netty,本篇将通过一个简单的应用来了解Netty的使用. 二.Netty应用 2.1 服务端客户端框架图 下图展示了Netty中服务端与 ...

  9. [原创]普通的MySQL多表连接查询

  10. 《快学Scala》——数组、映射和元组

    数组 定长数组:在Scala中可以用Array,初始化一个定长数组.例如: val nums = new Array[Int](10) //10个整数的数组,所有元素初始化为0 val a = new ...