[LeetCode] 673. Number of Longest Increasing Subsequence
来源:互联网 发布:firewallcmd端口转发 编辑:程序博客网 时间:2024/06/06 19:59
题目链接: https://leetcode.com/problems/number-of-longest-increasing-subsequence/description/
Description
Given an unsorted array of integers, find the number of longest increasing subsequence.
Example 1:
Input: [1,3,5,4,7]Output: 2Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7].
Example 2:
Input: [2,2,2,2,2]Output: 5Explanation: The length of longest continuous increasing subsequence is 1, and there are 5 subsequences' length is 1, so output 5.
Note: Length of the given array will be not exceed 2000 and the answer is guaranteed to be fit in 32-bit signed int.
解题思路
动态规划求解,建立两个数组 len
、cnt
:
* len[k]
: 表示以 nums[k]
为末尾的最长子序列长度。
* cnt[k]
: 表示以 nums[k]
为末尾的最长子序列个数。
对于每一个 nums[i]
,只需要遍历其前面的所有数字 nums[j]
(0 <= j < i
) ,找到比它小且长度最长的 nums[k]
,就可得出以 nums[i]
为末尾的子序列的最大长度 len[i] = len[k] + 1
。同时,以 nums[i]
为末尾的最长子序列个数应该等于 nums[j]
(0 <= j < i
) 中,比它小且长度最长的所有 nums[k]
的最长子序列个数之和。
用两条公式来阐述上面一段难以理解的话
* len[k] = max(len[k], len[i] + 1), for all 0 <= i < k and nums[i] < nums[k]
* cnt[k] = sum(cnt[i]), for all 0 <= i < k and len[k] = len[i] + 1
举个例子
nums = {1, 3, 5, 4, 7}
初始状态,len = {1, 1, 1, 1, 1}
,cnt = {1, 1, 1, 1, 1}
开始遍历,
* 数字 3,比它小的只有 1 => len[1] = len[0] + 1 = 2
,cnt[1] = cnt[0] = 1
;
* 数字 5,比它小且长度最长的为 3 => len[2] = len[1] + 1 = 3
,cnt[2] = cnt[1] = 1
;
* 数字 4, 比它小且长度最长的为 3 => len[3] = len[1] + 1 = 3
,cnt[3] = cnt[1] = 1
;
* 数字 7,比它小且长度最长的为 5 和 4 => len[4] = len[2] + 1 = 4
,cnt[4] = cnt[2] + cnt[3] = 2
最终状态,len = {1, 2, 3, 3, 4}
,cnt = {1, 1, 1, 1, 2}
Code
class Solution {public: int findNumberOfLIS(vector<int>& nums) { int maxLen = 1; int res = 0; vector<int> len(nums.size(), 1); vector<int> cnt(nums.size(), 1); for (int i = 1; i < nums.size(); ++i) { for (int j = 0; j < i; ++j) { if (nums[j] < nums[i]) { if (len[j] + 1 > len[i]) { len[i] = len[j] + 1; cnt[i] = cnt[j]; } else if (len[j] + 1 == len[i]) { cnt[i] += cnt[j]; } } } maxLen = max(maxLen, len[i]); } for (int i = 0; i < nums.size(); ++i) { if (maxLen == len[i]) res += cnt[i]; } return res; }};
- leetcode 673. Number of Longest Increasing Subsequence
- [LeetCode] 673. Number of Longest Increasing Subsequence
- LeetCode 673.Number of Longest Increasing Subsequence
- Leetcode | Number of Longest Increasing Subsequence
- leetcode 673. Number of Longest Increasing Subsequence 动态规划DP
- 673. Number of Longest Increasing Subsequence
- 673. Number of Longest Increasing Subsequence
- 673. Number of Longest Increasing Subsequence
- 673. Number of Longest Increasing Subsequence
- 673. Number of Longest Increasing Subsequence
- 673. Number of Longest Increasing Subsequence
- Number of Longest Increasing Subsequence
- Leetcode算法学习日志-673 Number of Longest Increasing Subsequence
- Leetcode 673 Number of Longest Increasing Subsequence(第六周作业)
- LeetCode week 9 : Number of Longest Increasing Subsequence
- LWC 49:673. Number of Longest Increasing Subsequence
- (M)Dynamic Programming:673. Number of Longest Increasing Subsequence
- 673. Number of Longest Increasing Subsequence【Medium】 一维动归
- 心理学-在对的时间遇见对的人,不负自己,不负时光。
- stm8-GPIO支持模式
- C++面试-常考知识点总结
- 数据结构基础
- 非监督特征学习与深度学习(一)----线性回归
- [LeetCode] 673. Number of Longest Increasing Subsequence
- javascript json字符串与对象相互转换
- JDK的配置(留存备用)
- Android ScrollView初始位置不在顶部的解决方法
- iPhone 开发分辨率 持续更新
- 0 1 矩阵查找最大正方形
- MySQL中的CONCAT、CONCAT_WS、GROUP_CONCAT函数,实现多行和多列的内容放在一个单元格内
- 第二十五篇 jQuery 学习7 获取并设置 CSS 类
- 灰度图像的腐蚀与膨胀