-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
99 lines (93 loc) · 2.04 KB
/
main.cpp
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
// 使用 lower_bound
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
vector<int> res;
for(int i = 0; i < nums.size(); ++i)
{
auto it = std::lower_bound(res.begin(), res.end(), nums[i]);
if(it == res.end())
res.push_back(nums[i]);
else *it = nums[i];
}
return res.size();
}
};
// 二分 + 贪心
/*
class Solution {
public:
int binary(vector<int> &res, int r, int x)
{
int l = 0, mid;
while(l <= r)
{
mid = (l + r) >> 1;
if(res[mid] > x)
{
r = mid - 1;
}
else if(res[mid] < x)
{
l = mid + 1;
}
else{
return mid;
}
}
return l;
}
int lengthOfLIS(vector<int>& nums) {
if(nums.size() < 1) return 0;
vector<int> res(nums.size(), INT_MIN);
int ans = 0;
res[0] = nums[0];
for(int i = 1; i < nums.size(); ++i)
{
if(nums[i] > res[ans])
{
res[++ans] = nums[i];
}
else
{
res[binary(res, ans, nums[i])] = nums[i];
}
}
return ans + 1;
}
};
*/
// dp dp[i]表示前i个数以nums[i]结尾的最长上升子序列
/*
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
vector<int> dp(nums.size(), 1);
int res = 0;
for(int i = 0; i < nums.size(); ++i)
{
for(int j = 0; j < i; ++j)
{
if(nums[i] > nums[j])
{
dp[i] = max(dp[i], dp[j]+1);
}
}
res = max(res, dp[i]);
}
return res;
}
};
*/
int main()
{
Solution s;
vector<int> nums = {3, 1, 2, 6, 4, 5, 10, 7};
cout << s.lengthOfLIS(nums) << endl;
return 0;
}