标签:

动态规划

描述:

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. 批量插入或更新操作之ON DUPLICATE KEY UPDATE用法

    实际的开发过程中,可能会遇到这样的需求,先判断某一记录是否存在,如果不存在,添加记录,如果存在,则修改数据.在INSERT语句末尾指定ON DUPLICATE KEY UPDATE可以解决这类问题. ...

  2. mybtais分批insert

    这里自己写了个对集合按一批的数量进行分批操作的分页bean,见PagenationUtil如下: package com.util; import java.util.List ; /** * @au ...

  3. Zookeeper的安装与使用:

  4. [转]Visual Studio 2010单元测试(2)--运行测试并查看代码覆盖率

    Visual Studio 2010 单元测试--运行测试并查看代码覆盖率 运行测试并查看代码覆盖率对程序集中的代码运行测试时,可以通过收集代码覆盖率数据来查看正在测试的项目代码部分. 运行测试并查看 ...

  5. mapreduce.Job: Running job: job_1553100392548_0001

    这几天一直在尝试一个mapreduce的例子,但是一直都是卡在mapreduce.Job: Running job: job_1553100392548_0001,查看日志也不报错,查看每个配置文件没 ...

  6. thinkcmf报错:fileowner(): stat failed for /sys

    thinkcmf转移到linux云服务器后,后台更新缓存页面报错,错误信息fileowner(): stat failed for /sys 临时解决办法:修改common.php cmf_clear ...

  7. 处理iphone的 .play() 不能播放问题

    一.添加音乐 <audio id="Jaudio" src="shake.mp3" preload loop="loop" contr ...

  8. Git同平台下多个账号配置

    在公司要使用公司和自己的两个账号都往GitHub上面提交,所以整理成笔记 具体配置项 StrictHostKeyChecking no UserKnownHostsFile /dev/null # 为 ...

  9. Linux时间介绍

    Linux时钟分为系统时钟(System Clock)和硬件(Real Time Clock,简称RTC)时钟.系统时钟是指当前Linux Kernel中的时钟,而硬件时钟则是主板上由电池供电的时钟, ...

  10. 理解 Python 语言中的 defaultdict

    众所周知,在Python中如果访问字典中不存在的键,会引发KeyError异常(JavaScript中如果对象中不存在某个属性,则返回undefined).但是有时候,字典中的每个键都存在默认值是非常 ...