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

解题思路

动态规划求解,建立两个数组 lencnt
* 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 = 2cnt[1] = cnt[0] = 1
* 数字 5,比它小且长度最长的为 3 => len[2] = len[1] + 1 = 3cnt[2] = cnt[1] = 1
* 数字 4, 比它小且长度最长的为 3 => len[3] = len[1] + 1 = 3cnt[3] = cnt[1] = 1
* 数字 7,比它小且长度最长的为 5 和 4 => len[4] = len[2] + 1 = 4cnt[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;    }};
阅读全文
0 0
原创粉丝点击