-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathleetcode300_longest_increasing_subsequence.cpp
74 lines (60 loc) · 1.57 KB
/
leetcode300_longest_increasing_subsequence.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
/**
* @file leetcode300_longest_increasing_subsequence.cpp
* @author wangguibao(https://github.com/wangguibao)
* @date 2023/06/09 21:02
* @brief https://leetcode.com/problems/longest-increasing-subsequence
*
**/
#include <iostream>
#include <vector>
#include <climits>
using namespace std;
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
size_t len = nums.size();
std::vector<int> lis(len);
lis[0] = 1;
for (size_t i = 1; i < len; ++i) {
int cur_max = 1;
for (size_t j = 0; j < i; ++j) {
if (nums[i] > nums[j]) {
if (cur_max < (lis[j] + 1)) {
cur_max = (lis[j] + 1);
}
}
}
lis[i] = cur_max;
}
int m = lis[0];
for (size_t i = 1; i < len; ++i) {
if (m < lis[i]) {
m = lis[i];
}
}
return m;
}
};
int main() {
int n;
std::vector<int> nums;
while (1) {
n = 0;
std::cout << "Number of elements: ";
std::cin >> n;
if (n <= 0) {
return 0;
}
std::cout << n << " integers: " << std::endl;
nums.clear();
int ele;
for (int i = 0; i < n; ++i) {
std::cin >> ele;
nums.push_back(ele);
}
Solution solution;
size_t len = solution.lengthOfLIS(nums);
std::cout << len << std::endl;
}
return 0;
}