-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlengthOfLIS.cpp
More file actions
32 lines (31 loc) · 779 Bytes
/
lengthOfLIS.cpp
File metadata and controls
32 lines (31 loc) · 779 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Binary Search
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
vector<int> lis({nums[0]});
for (int e: nums) {
if (lis.back() < e) lis.push_back(e);
else {
auto it = lower_bound(lis.begin(), lis.end(), e);
*it = e;
}
}
return lis.size();
}
};
// DP
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
int ans = 1;
vector<int> dp(nums.size(), 1);
for (int i = 1; i < nums.size(); ++i) {
for (int j = 0; j < i; ++j) {
if (nums[j] < nums[i])
dp[i] = std::max(dp[i], dp[j] + 1);
}
ans = std::max(ans, dp[i]);
}
return ans;
}
};