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 ...
随机推荐
- 《DSP using MATLAB》Problem 8.12
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- 《DSP using MATLAB》Problem 8.10
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- 在当前目录打开DOS命令行窗口
[step1]选中文件夹 [step2]shift + 鼠标右键
- TFS2013 微软源代码管理工具 安装与使用图文教程
最近公司新开发一个项目要用微软的TFS2013进行项目的源代码管理,以前只是用过SVN,从来没有用过TFS,所以在网上百度.谷歌了好一阵子来查看怎么安装和配置,还好花了一天时间总算是初步的搞定了,下面 ...
- 深入浅出 Java Concurrency (8): 锁机制 part 3[转]
接上篇,这篇从Lock.lock/unlock开始.特别说明在没有特殊情况下所有程序.API.文档都是基于JDK 6.0的. public void java.util.concurrent.lock ...
- 稀疏表示step by step(转)
原文地址:稀疏表示step by step(转)作者:野火春风 稀疏表示step by step(1) 声明:本人属于绝对的新手,刚刚接触“稀疏表示”这个领域.之所以写下以下的若干个连载,是鼓 ...
- 分享一个百度大牛的Python视频系列下载
好像是百度资深大数据工程师 在录制Python视频课程讲课,包括Python基础入门.数据分析.网络爬虫.大数据处理.机器学习.推荐系统等系列,他还在不停地录制,课程感觉很不错,视频网盘分享给大家 学 ...
- 阿里面试题,为什么wait()方法要放在同步块中?
某天我在***的时候,突然有个小伙伴微信上说:“哥,阿里面试又又挂了,被问到为什么wait()方法要放在同步块中,没答出来!” 我顿时觉得**一紧,仔细回顾一下,如果wait()方法不在同步块中,代码 ...
- Luogu P1730 最小密度路径(最短路径+dp)
P1730 最小密度路径 题面 题目描述 给出一张有 \(N\) 个点 \(M\) 条边的加权有向无环图,接下来有 \(Q\) 个询问,每个询问包括 \(2\) 个节点 \(X\) 和 \(Y\) , ...
- Leetcode238. Product of Array Except Self除自身以外数组的乘积
给定长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积. 示例: 输入: [1 ...