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 参考代码:

 1  public int longestIncreasingSubsequence(int[] nums) {
 2         // write your code here
 3         if(nums.length==0){
 4             return 0;
 5         }
 6         int[] dp = new int[nums.length];
 7         int max = 0;
 8
 9         for(int i = 0; i<nums.length; i++){
10         dp[i] =1;
11         for(int j = 0; j<i; j++){
12             if(nums[j]<nums[i]){
13                 dp[i]=dp[i]<dp[j]+1?dp[j]+1:dp[i];
14             }
15
16         }
17          if(dp[i]>max){
18                 max = dp[i];
19             }
20
21         }
22         return max;
23     }
时间: 2024-11-13 17:17:13

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

LintCode刷题笔记(九章ladder PartOne)--BugFree

九章ladder的前半部分刷题笔记,在这次二刷的时候补上~ @ 2017.05.21 141 - sqrtx 二分答案 ---  binarySearch二分法 --- class Solution: """ @param x: An integer @return: The sqrt of x """ def sqrt(self, x): # write your code here if not x: return 0 start, end

LintCode刷题笔记-- LongestCommonSquence

题目描述: Given two strings, find the longest common subsequence (LCS). Your code should return the length of LCS. 解题思路: 这一题是非常经典的动态规划问题,在解题思路上可以按照经典的动态规划的解法,这是在系统学习动态规划之后第一个解决的LintCode上的问题: 1.子问题划分 给出两个字符串的A,B,两个字符串长度分别为lenA,lenB,求出两个字符串的LCS: 这划分为子问题:A.

lintcode 容易题:Longest Increasing Continuous subsequence 最长上升连续子序列

题目: 最长上升连续子序列 给定一个整数数组(下标从 0 到 n-1, n 表示整个数组的规模),请找出该数组中的最长上升连续子序列.(最长上升连续子序列可以定义为从右到左或从左到右的序列.) 样例 给定 [5, 4, 2, 1, 3], 其最长上升连续子序列(LICS)为 [5, 4, 2, 1], 返回 4. 给定 [5, 1, 2, 3, 4], 其最长上升连续子序列(LICS)为 [1, 2, 3, 4], 返回 4. 解题: 这个直接找就可以了,最长的升序,和最长的降序,再求最大值,时

LintCode刷题笔记-- Distinct Subsequences

题目描述: Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the rel

LintCode刷题笔记-- Maximal Square

题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area. Example For example, given the following matrix: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Return 4. 解题思路: 1.这一题明显使用动态规划来解题,开始想法使用动态

LintCode刷题笔记-- BackpackIV

动态规划 描述: Given an integer array nums with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target. Example Given nums = [1, 2, 4], target = 4 The possible combination ways are: [1, 1,

LintCode刷题笔记-- PaintHouse 1&amp;2

动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color. The

LintCode刷题笔记-- Maximum Product Subarray

动态规划 描述: Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest product = 6. 解题思路: 前面几道题有点儿过分依赖答案了,后面还是需要自己主动

LintCode刷题笔记-- BackpackII

标记: 动态规划 问题描述: Given n items with size Ai, an integer m denotes the size of a backpack. How full you can fill this backpack? Example If we have 4 items with size [2, 3, 5, 7], the backpack size is 11, we can select [2, 3, 5], so that the max size we