From f5ca3662ff0f17a46531d4d423772f3258031b30 Mon Sep 17 00:00:00 2001 From: Kiritow <1362050620@qq.com> Date: Wed, 17 Nov 2021 15:38:10 +0000 Subject: [PATCH] Add leetcode 673 --- LeetCode-CN/673.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 LeetCode-CN/673.cpp diff --git a/LeetCode-CN/673.cpp b/LeetCode-CN/673.cpp new file mode 100644 index 0000000..740ef4b --- /dev/null +++ b/LeetCode-CN/673.cpp @@ -0,0 +1,43 @@ + +class Solution { +public: + int findNumberOfLIS(vector& nums) { + int N = nums.size(); + vector> dp(N); // 以i为结尾的LIS长度, 这个长度出现的次数 + for (int i = 0; i < N; i++) { + dp[i] = { 1,1 }; + } + for (int i = 1; i < N; i++) { + int count = 1; + int maxLastLen = 0; + for (int j = 0; j < i; j++) { + if (nums[i] > nums[j]) { + if (dp[j].first == maxLastLen) + { + count += dp[j].second; + } + else if (dp[j].first > maxLastLen) { + count = dp[j].second; + maxLastLen = dp[j].first; + } + } + } + dp[i].first = maxLastLen + 1; + dp[i].second = count; + } + + int maxFirst = 0; + for (auto& p : dp) { + maxFirst = max(maxFirst, p.first); + } + + int cntSecond = 0; + for (auto& p : dp) { + if (p.first == maxFirst) { + cntSecond += p.second; + } + } + + return cntSecond; + } +};