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             for(int i = 1;i < sz;i ++)
12             {
13                 for(int j = 0;j < i;j ++)
14                 {
15                     if(nums[i]>nums[j]&&dp[j].first+1>dp[i].first)
16                     {
17
18                         dp[i].first = dp[j].first+1;
19                         dp[i].second = dp[j].second;
20                         LISlen = max(LISlen,dp[i].first);
21                     }
22                     else if(nums[i]>nums[j]&&dp[j].first+1==dp[i].first)
23                     {
24                         dp[i].second += dp[j].second;
25                     }
26                 }
27             }
28
29             int rnt = 0;
30             _for(i,0,sz)
31                 if(dp[i].first==LISlen)
32                     rnt += dp[i].second;
33             return rnt;
34         }
35 };

原文地址:https://www.cnblogs.com/Asurudo/p/10354420.html

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

Leetcode-673 (Number of 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] 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

[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

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

673. 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

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

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(

LeetCode Number of Longest Increasing Subsequence

原题链接在这里:https://leetcode.com/problems/number-of-longest-increasing-subsequence/description/ 题目: 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 longe