Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4d8ecb8

Browse files
zhangzz2015gitbook-bot
authored andcommitted
GitBook: [greyireland#110] No subject
1 parent 57ff5b0 commit 4d8ecb8

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

‎basic_algorithm/dp.md‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,62 @@ public:
512512
};
513513
```
514514

515+
### [Number of Longest Increasing Subsequence](https://leetcode.com/problems/number-of-longest-increasing-subsequence/)
516+
517+
```cpp
518+
// Some code
519+
// dp method.
520+
class Solution {
521+
public:
522+
int findNumberOfLIS(vector<int>& nums) {
523+
524+
// use dp method.
525+
// dp1[i] the end of i maxLengh. Loop j from 0 to i-1. if(nums[i]>nums[j] )
526+
// if(dp2[j]+1>maxNum) update maxNum maxLengh.
527+
// dp2[i] total num when the end of i LIS is equal maxLengh.
528+
// Time O(n^2) Space O(n) Use 15min.
529+
vector<pair<int,int>> dp(nums.size());
530+
dp[0].first = 1; // maxLengh = 1
531+
dp[0].second = 1;// maxNum = 1;
532+
int totalMaxLen = 1;
533+
for(int i =1; i< nums.size(); i++ )
534+
{
535+
int maxLength=1;
536+
int maxNum = 1;
537+
for(int j=0; j< i; j++)
538+
{
539+
if(nums[i]>nums[j])
540+
{
541+
if(dp[j].first+1>maxLength)
542+
{
543+
maxNum = dp[j].second;
544+
maxLength = dp[j].first+1;
545+
}
546+
else if(dp[j].first+1==maxLength)
547+
{
548+
maxNum += dp[j].second;
549+
}
550+
}
551+
}
552+
dp[i].first = maxLength;
553+
dp[i].second = maxNum;
554+
totalMaxLen = max(totalMaxLen, maxLength);
555+
}
556+
int ret =0;
557+
for(int i=0; i< nums.size(); i++)
558+
{
559+
if(dp[i].first == totalMaxLen)
560+
{
561+
ret += dp[i].second;
562+
}
563+
}
564+
return ret;
565+
566+
567+
}
568+
};
569+
```
570+
515571
### [word-break](https://leetcode-cn.com/problems/word-break/)
516572
517573
> 给定一个**非空**字符串 _s_ 和一个包含**非空**单词列表的字典 _wordDict_,判定 _s_ 是否可以被空格拆分为一个或多个在字典中出现的单词。

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /