Leetcode 673 Solution

This article provides solution to leetcode question 673 (number-of-longest-increasing-subsequence)

https://leetcode.com/problems/number-of-longest-increasing-subsequence

Solution

class Solution {
public:
    int findNumberOfLIS(vector<int>& nums) {
        if (nums.size() == 0)
            return 0;

        vector<int> ms(nums.size(), 1);
        vector<int> ms_cnt(nums.size(), 1);

        int max_len = 0;
        int max_cnt = 0;

        for (int i = 0; i < nums.size(); i++)
        {
            for (int j = 0; j < i; j++)
            {
                if (nums[i] > nums[j])
                {
                    if (ms[i] < ms[j] + 1)
                    {
                        ms[i] = ms[j] + 1;
                        ms_cnt[i] = ms_cnt[j];
                    }
                    else if (ms[i] == ms[j] + 1)
                        ms_cnt[i] += ms_cnt[j];
                }
            }

            if (max_len < ms[i])
            {
                max_len = ms[i];
                max_cnt = ms_cnt[i];
            }
            else if (max_len == ms[i])
                max_cnt += ms_cnt[i];
        }

        return max_cnt;
    }
};