LeetCode. 673. 最长递增子序列的个数(动态规划 + 状态累计)

#include <vector>
#include <iostream>
using namespace std;
class Solution {
public:
	int findNumberOfLIS(vector<int>& nums) {
		pair<int, int> result(1, 0);										//长度 + 数量
		vector<pair<int, int>> count(nums.size(), pair<int, int>(1, 1));	//每个位置的最长递增子序列长度 + 数量
		for (int i = 0; i < nums.size(); ++i)
		{
			for (int j = 0; j < i; ++j) {
				//左边的数字 小于 当前位置的数字 && 左边数字的位置的LIS + 1 >= 当前位置的LIS
				if (nums[j] < nums[i] && count[j].first + 1 >= count[i].first) {
					//如果LIS相同,则将相同LIS的数量进行累加
					if (count[i].first == count[j].first + 1) {
						count[i].second += count[j].second;
					}
					//LIS不相同,则更新大的LIS并且更新LIS的数量
					else {
						count[i].first = count[j].first + 1;
						count[i].second = count[j].second;
					}
				}
			}

			//不断记录LIS 以及 重复的个数
			//更新LIS和数量
			if (result.first < count[i].first) {
				result.first = count[i].first;
				result.second = count[i].second;
			}
			//最大的LIS相同,则累加数量
			else if (result.first == count[i].first)
				result.second += count[i].second;
		}
		return result.second;
	}
};
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值