标签:

动态规划

描述:

Given a sequence of integers, find the longest increasing subsequence (LIS).

You code should return the length of the LIS.

Clarification

What's the definition of longest increasing subsequence?

  • The longest increasing subsequence problem is to find a subsequence of a given sequence in which the subsequence's elements are in sorted order, lowest to highest, and in which the subsequence is as long as possible. This subsequence is not necessarily contiguous, or unique.

解题思路:

经过将近四个小时的痛苦思考,最终还是无奈求助于答案。看来不看答案AC动态规划的问题还是需要一个过程的。真心羡慕饼王那样的大神(北大数学系的牛逼人士,数学功底真的杠杠的),言归正传,这一道题是求最大增序列,可以不连续,但是必须是递增。还是使用动态规划来解决这一问题:

1.划分子问题:

这一步骤基本算是考虑到了,例如一个长度为n的数列,可以划分为1->n的LIS,2->n的LIS,3->n的LIS。。。。n-1->n的LIS,但是这种解法存在问题,在于你在计算一个子问题的时候会出现多种增长序列,导致解不唯一。所以,划分子问题的时候应当将子序列的尾坐标依次从2到n,即1->2, 1->3, 1->4....1->n-1,1->n。之后再在每一个子问题中求出递增序列

2.初始状态的定义:

对于总的问题,可以定义一个max来作为存结果的变量

对于各个子问题,可以定义一个dp[]来对于先前每个子问题的结果进行记录(备忘录),并且在每个子问题开始前初始化为1,每种子问题最短也会有1.

3.子问题与递进问题的关系(递推公式):

在每个子问题中最大的值可以设置为子序列的最后一个元素即nums[i],只要存在先前元素小于最后一个元素即nums[j]<nums[i],说明在j到i呈增长趋势,如果当前的最长长度若小于在j点的最长长度+1(在增长序列上再加上1),则该子问题的最长长度变为dp[j]+1.若大于的话说i与j之间存在下降趋势。

公式为:

k[i] = k[i] (k[i]<K[j]+1)

K[i] = K[j]+1 (k[i]>K[j]+1)

4 参考代码:

  public int longestIncreasingSubsequence(int[] nums) {
// write your code here
if(nums.length==0){
return 0;
}
int[] dp = new int[nums.length];
int max = 0; for(int i = 0; i<nums.length; i++){
dp[i] =1;
for(int j = 0; j<i; j++){
if(nums[j]<nums[i]){
dp[i]=dp[i]<dp[j]+1?dp[j]+1:dp[i];
} }
if(dp[i]>max){
max = dp[i];
} }
return max;
}

LintCode刷题笔记--Longest Increasing Subsequence的更多相关文章

  1. [刷题] 300 Longest Increasing Subsequence

    要求 给定一个整数序列,求其中的最长上升子序列长度 子序列元素可不相邻 元素相等不算上升 一个序列可能有多个最长上升子序列,但最长的长度只有一个 思路 暴力解法:选择所有子序列进行判断((2^n)*n ...

  2. LintCode刷题笔记-- LongestCommonSquence

    标签:动态规划 题目描述: Given two strings, find the longest common subsequence (LCS). Your code should return ...

  3. lintcode刷题笔记(一)

    最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...

  4. LintCode刷题笔记-- Distinct Subsequences

    标签:动态规划 题目描述: Given a string S and a string T, count the number of distinct subsequences of T in S. ...

  5. 2017四川省赛E题( Longest Increasing Subsequence)

    提交地址: https://www.icpc-camp.org/contests/4rgOTH2MbOau7Z 题意: 给出一个整数数组,F[i]定义为以i结尾的最长上升子序列,然后问以此删除掉第i个 ...

  6. LintCode刷题笔记-- PaintHouse 1&2

    标签: 动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The ...

  7. LintCode刷题笔记-- Maximum Product Subarray

    标签: 动态规划 描述: Find the contiguous subarray within an array (containing at least one number) which has ...

  8. LintCode刷题笔记-- Maximal Square

    标签:动态规划 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing a ...

  9. LintCode刷题笔记-- Edit distance

    标签:动态规划 描述: Given two words word1 and word2, find the minimum number of steps required to convert wo ...

随机推荐

  1. 04_jQuery对象初识(三)

    <div id="d1"> <p><span>span</span></p> <div>div</di ...

  2. linux命令补全工具

    一:下载bash-competion工具 https://files.cnblogs.com/files/zgngg/bash-completion.zip 二:解压  unzip bash-comp ...

  3. 07-img和a标签

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. java笔试之计算n x m的棋盘格子

    请编写一个函数(允许增加子函数),计算n x m的棋盘格子(n为横向的格子数,m为竖向的格子数)沿着各自边缘线从左上角走到右下角,总共有多少种走法,要求不能走回头路,即:只能往右和往下走,不能往左和往 ...

  5. 不同版本springboot端点开启方法

    1.X版本与2.X区别:2.X大部分路径要加“/actuator” 端点列表(注意最后一栏Sensitive为true的端点如果不设置为false将不可访问): https://docs.spring ...

  6. HBase Master-status

  7. 深度神经网络Google Inception Net-V3结构图

    深度神经网络Google Inception Net-V3结构图 前言 Google Inception Net在2014年的 ImageNet Large Scale Visual Recognit ...

  8. List -- 循环操作

    1,单元循环 for…in 2,索引循环 for…in range(len(List)) 3,同时循环单元和索引 使用enumerate: for index, item in enumerate(L ...

  9. JZOJ100048 【NOIP2017提高A组模拟7.14】紧急撤离

    题目 题目大意 给你一个01矩阵,每次询问从一个点是否可以走到另一个点. 每次走只能往右或者往下. 思考历程 这题啊,我想的时候真的是脑洞大开-- 首先,我一眼看下去,既然要询问是否联通,那么能不能求 ...

  10. Html5 拨打手机号码

    采用url链接的方式,实现拨打电话功能. 1.最常用WEB页面JS实现一键拨号的电话拨打功能: <a href="tel:12345678987">WEB页面JS拨打& ...