leetcode300. Longest Increasing Subsequence 最长递增子序列

class Solution {
public:
    int lengthOfLIS(vector<int>& nums) {
        int length = nums.size();
        if(length <= 0)
            return 0;
        vector<int> result(length);
        for(int i = 0;i < length;i++)
            result[i] = 1;
        for(int i = 1;i < length;i++){
            int max_sum = 1;
            for(int j = i-1;j >= 0;j--){
                if(nums[i] > nums[j]){
                    int num = result[j] + 1;
                    if(num > max_sum)
                        max_sum = num;
                }
            }
            result[i] = max_sum;
        }
        int max = 1;
        for(int i = 0;i < length;i++){
            if(result[i] > max)
                max = result[i];
        }
        return max;
    }
};

原文地址:https://www.cnblogs.com/ymjyqsx/p/9567417.html

时间: 2024-10-08 05:00:30

leetcode300. Longest Increasing Subsequence 最长递增子序列的相关文章

LeetCode 673. Number of Longest Increasing Subsequence 最长递增子序列的个数 (C++/Java)

题目: Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: Input: [1,3,5,4,7] Output: 2 Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7]. Example 2: Input: [2,2,2,2,2] O

[leetcode]300. Longest Increasing Subsequence最长递增子序列

Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Input: [10,9,2,5,3,7,101,18] Output: 4 Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4. Note: There may be more

Leetcode-673 (Number of Longest Increasing Subsequence)最长递增子序列的个数

1 #define _for(i,a,b) for(int i = (a);i < (b);i ++) 2 class Solution 3 { 4 public: 5 int findNumberOfLIS(vector<int>& nums) 6 { 7 int sz = nums.size(); 8 vector<pair<int,int>> dp (sz,{1,1});//LISlen times 9 10 int LISlen = 1; 11 f

LeetCode 300. Longest Increasing Subsequence最长上升子序列 (C++/Java)

题目: Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Input: [10,9,2,5,3,7,101,18] Output: 4 Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4. Note: There may be

[LeetCode] Number of Longest Increasing Subsequence 最长递增序列的个数

Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: Input: [1,3,5,4,7] Output: 2 Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7]. Example 2: Input: [2,2,2,2,2] Outpu

POJ 2533 - Longest Ordered Subsequence - [最长递增子序列长度][LIS问题]

题目链接:http://poj.org/problem?id=2533 Time Limit: 2000MS Memory Limit: 65536K Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ...,

poj 2533 Longest Ordered Subsequence 最长递增子序列(LIS)

两种算法 1.  O(n^2) 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 6 int a[1005]; 7 int dp[1005]; 8 int main() 9 { 10 int n, maxn; 11 while(scanf("%d", &n) != EOF) 12 { 13 maxn = 0; 14 for(

POJ 2533 Longest Ordered Subsequence 最长递增序列

Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK), where 1 <= i1 < i2 < ... < iK <= N. For example, seque

LCS(Longest Common Subsequence 最长公共子序列)

问题描述 最长公共子序列,英文缩写为LCS(Longest Com #include <bits/stdc++.h> const int MAX=1010; char x[MAX]; char y[MAX]; int DP[MAX][MAX]; int b[MAX][MAX]; using namespace std; int PRINT_LCS(int b[][MAX],char *x,int i,int j) { if(i==0||j==0) return 1; if(b[i][j]==1