LeetCode | 673. Number of Longest Increasing Subsequence中等偏难题 找寻数组里面最大长度的子串的个数
2018-01-18 17:58
531 查看
Givenan unsorted array of integers, find the number of longest increasingsubsequence.Example1:Input: [1,3,5,4,7]
Output: 2
Explanation: The two longest increasing subsequenceare [1, 3, 4, 7] and [1, 3, 5, 7].
Example2:Input: [2,2,2,2,2]
Output: 5
Explanation: The length of longest continuousincreasing subsequence is 1, and there are 5 subsequences' length is 1, sooutput 5.
Note: Length of the given array will be not exceed 2000 and theanswer is guaranteed to be fit in 32-bit signed int.这是一道最长子序列题,子序列中的每一个数在原序列中不一定是相邻的,这一点要注意,用dp 解决,用dp[i]记录最后一个数字为nums[i]的最长子序列长度,用count[i]记录最后一个数字为nums[i]的最长子序列个数,
dp[i]=max(dp[j]+1),{j| j=0,1,2…i-1&&nums[j]<nums[i]},
count[i]=所有count[j]之和if(dp[j]+1==dp[i])
theAllMax代表dp中最大的那个值
最后dp
的值代表最长的子序列长度,for(eachint u in dp),当dp[u]== theAllMax的时候,sum+=count[u],最后sum的值就是最长子序列的个数class Solution {
public:
int findNumberOfLIS(vector<int>& nums) {
int n = nums.size();
if (n == 0) return 0;
vector<int> dp(n);
vector<int> count(n, 0);
dp[0] = 1;
count[0] = 1;
int tmp,tmpCount;
int theAllMax = 1, theAllMaxCount=0;
for (int i = 1; i < n; i++)
{
tmp = 0; tmpCount = 0;
for (int j = i - 1; j >= 0; j--)
{
int t;
if (nums[i] > nums[j])
{
t = dp[j] + 1;
if (t > tmp) {
tmp = t;
tmpCount = count[j];
}
else if(t==tmp)
{
tmpCount += count[j];
}
}
}
if (tmp == 0) {
tmp = 1;
tmpCount = 1;
}
dp[i] = tmp;
count[i] = tmpCount;
if (tmp > theAllMax) {
theAllMax = tmp;
}
}
for (int i = 0; i < n; i++)
{
if (dp[i] == theAllMax)
theAllMaxCount += count[i];
}
return theAllMaxCount;
}
};
Output: 2
Explanation: The two longest increasing subsequenceare [1, 3, 4, 7] and [1, 3, 5, 7].
Example2:Input: [2,2,2,2,2]
Output: 5
Explanation: The length of longest continuousincreasing subsequence is 1, and there are 5 subsequences' length is 1, sooutput 5.
Note: Length of the given array will be not exceed 2000 and theanswer is guaranteed to be fit in 32-bit signed int.这是一道最长子序列题,子序列中的每一个数在原序列中不一定是相邻的,这一点要注意,用dp 解决,用dp[i]记录最后一个数字为nums[i]的最长子序列长度,用count[i]记录最后一个数字为nums[i]的最长子序列个数,
dp[i]=max(dp[j]+1),{j| j=0,1,2…i-1&&nums[j]<nums[i]},
count[i]=所有count[j]之和if(dp[j]+1==dp[i])
theAllMax代表dp中最大的那个值
最后dp
的值代表最长的子序列长度,for(eachint u in dp),当dp[u]== theAllMax的时候,sum+=count[u],最后sum的值就是最长子序列的个数class Solution {
public:
int findNumberOfLIS(vector<int>& nums) {
int n = nums.size();
if (n == 0) return 0;
vector<int> dp(n);
vector<int> count(n, 0);
dp[0] = 1;
count[0] = 1;
int tmp,tmpCount;
int theAllMax = 1, theAllMaxCount=0;
for (int i = 1; i < n; i++)
{
tmp = 0; tmpCount = 0;
for (int j = i - 1; j >= 0; j--)
{
int t;
if (nums[i] > nums[j])
{
t = dp[j] + 1;
if (t > tmp) {
tmp = t;
tmpCount = count[j];
}
else if(t==tmp)
{
tmpCount += count[j];
}
}
}
if (tmp == 0) {
tmp = 1;
tmpCount = 1;
}
dp[i] = tmp;
count[i] = tmpCount;
if (tmp > theAllMax) {
theAllMax = tmp;
}
}
for (int i = 0; i < n; i++)
{
if (dp[i] == theAllMax)
theAllMaxCount += count[i];
}
return theAllMaxCount;
}
};
相关文章推荐
- (Relax 后缀数组1.4)POJ 2774 Long Long Message(求两个字符串公共子串的最大长度)
- poj 3294 Life Forms(n个字符串中 求公共子串长度超过k得最大子串 后缀数组)
- 程序员面试金典——解题总结: 9.17中等难题 17.8给定一个整数数组(有正数有负数),找出总和最大的连续数列,并返回总和
- 【Leetcode】寻找数串中连续最大整数和且最大长度的子串
- POJ 2774-Long Long Message(后缀数组+高度数组-最大公共子串长度)
- 【Leetcode】寻找数串中连续最大整数和且最大长度的子串
- POJ 2217-Secretary(后缀数组+高度数组-最大公共子串长度)
- 【LeetCode】寻找最大长度不重复子串
- Leetcode(3)-找最大子串的长度
- leetcode_[Python/C++]_3_Longest Substring Without Repeating Characters(不重复子串最大长度)
- [LeetCode] Number of Subarrays with Bounded Maximum 有界限最大值的子数组数量
- 如果数组长度n为奇数,则将数组中最大的元素放到 output[] 数组最中间的位置
- [LeetCode] Maximum Average Subarray II 子数组的最大平均值之二
- Java整型数组的最大长度到底有多长?
- nyoj 983 ——首尾相连数组的最大子数组和——————【最大子串和变形】
- LeetCode 624. Maximum Distance in Arrays (在数组中的最大距离)$
- java中数组的最大长度以及List的最大容量
- [leetcode]获取字符串中字符不重复的最长子串的长度
- leetcode——Longest Substring Without Repeating Characters 求链表中无重复字符的最大字串长度(AC)
- POJ - 3415 Common Substrings(后缀数组求长度不小于 k 的公共子串的个数+单调栈优化)