LintCode刷题笔记--Longest Increasing Subsequence
标签:
动态规划
描述:
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的更多相关文章
- [刷题] 300 Longest Increasing Subsequence
要求 给定一个整数序列,求其中的最长上升子序列长度 子序列元素可不相邻 元素相等不算上升 一个序列可能有多个最长上升子序列,但最长的长度只有一个 思路 暴力解法:选择所有子序列进行判断((2^n)*n ...
- LintCode刷题笔记-- LongestCommonSquence
标签:动态规划 题目描述: Given two strings, find the longest common subsequence (LCS). Your code should return ...
- lintcode刷题笔记(一)
最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...
- LintCode刷题笔记-- Distinct Subsequences
标签:动态规划 题目描述: Given a string S and a string T, count the number of distinct subsequences of T in S. ...
- 2017四川省赛E题( Longest Increasing Subsequence)
提交地址: https://www.icpc-camp.org/contests/4rgOTH2MbOau7Z 题意: 给出一个整数数组,F[i]定义为以i结尾的最长上升子序列,然后问以此删除掉第i个 ...
- LintCode刷题笔记-- PaintHouse 1&2
标签: 动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The ...
- LintCode刷题笔记-- Maximum Product Subarray
标签: 动态规划 描述: Find the contiguous subarray within an array (containing at least one number) which has ...
- LintCode刷题笔记-- Maximal Square
标签:动态规划 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing a ...
- LintCode刷题笔记-- Edit distance
标签:动态规划 描述: Given two words word1 and word2, find the minimum number of steps required to convert wo ...
随机推荐
- java基础之System类
System类概述System 类包含一些有用的类字段和方法.它不能被实例化. 成员方法 public static void gc()运行垃圾回收器 public static void exit( ...
- 图数据库neo4j和关系数据库的区别
相信您和我一样,在使用关系型数据库时常常会遇到一系列非常复杂的设计问题.例如一部电影中的各个演员常常有主角配角之分,还要有导演,特效等人员的参与.通常情况下这些人员常常都被抽象为Person类型,对应 ...
- Ubuntu中使用Nginx+rtmp搭建流媒体直播服务
一.背景 本篇文章是继上一篇文章<Ubuntu中使用Nginx+rtmp模块搭建流媒体视频点播服务>文章而写,在上一篇文章中我们搭建了一个点播服务器,在此基础上我们再搭建一个直播服务器, ...
- CAS添加验证码功能
1. cas.war 下面的web-inf/web.xml lib添加 kaptcha.jar kaptcha.jar通过maven获取 <dependency> <group ...
- 如何 在 jQuery 中的 $.each 循环中使用 break 和 continue
jQuery中each类似于javascript的for循环 但不同于for循环的是在each里面不能使用break结束循环,也不能使用continue来结束本次循环,想要实现类似的功能就只能用ret ...
- HandlerInterceptorAdapter或HandlerInterceptor的使用
Spring拦截器 HandlerInterceptorAdapter需要继承,HandlerInterceptor需要实现 可以作为日志记录和登录校验来使用 建议使用HandlerIntercept ...
- C语言函数指针和回调函数
彻底搞定C指针-函数名与函数指针 函数名&函数名取地址 函数指针 通常我们可以将指针指向某类型的变量,称为类型指针(如,整型指针).若将一个指针指向函数,则称为函数指针. 函数名的意义 函数名 ...
- utils01_git的使用
1.git和的安装 下载git https://www.git-scm.com/download/ 完全下一步安装git和TortoiseGit和TortoiseGit的汉化包 2.右击选中小乌龟点击 ...
- Html5 拨打手机号码
采用url链接的方式,实现拨打电话功能. 1.最常用WEB页面JS实现一键拨号的电话拨打功能: <a href="tel:12345678987">WEB页面JS拨打& ...
- 留下来做项目经理还是跳槽学Java
毕业两年了,曾经给自己计划工作两年后跳一次槽,去尝试学习更多的东西.2012年7月5日入职,现在整整两年,最近面临这样的一个抉择:是留在公司继续做项目经理,还是跳槽去学习Java. 我的基本情况:本科 ...