forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
76 lines (56 loc) · 1.72 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
/// Source : https://leetcode.com/problems/longest-increasing-subsequence/description/
/// Author : liuyubobobo
/// Time : 2017-11-19
#include <iostream>
#include <vector>
using namespace std;
/// Memory Search
/// Time Complexity: O(n^2)
/// Space Complexity: O(n)
class Solution {
private:
// memo[i] is the length of the longest increasing sequence end in nums[i]
vector<int> memo;
int getMaxLength(const vector<int> &nums, int index){
if(memo[index] != -1)
return memo[index];
int res = 1;
for(int i = 0 ; i <= index-1 ; i ++)
if(nums[index] > nums[i])
res = max(res, 1 + getMaxLength(nums, i));
memo[index] = res;
return res;
}
public:
int lengthOfLIS(vector<int>& nums) {
if(nums.size() == 0)
return 0;
memo = vector<int>(nums.size(), -1);
int res = 1;
for(int i = 0 ; i < nums.size() ; i ++)
res = max(res, getMaxLength(nums, i));
return res;
}
};
int main() {
int nums1[] = {10, 9, 2, 5, 3, 7, 101, 18};
vector<int> vec1(nums1, nums1 + sizeof(nums1)/sizeof(int));
cout << Solution().lengthOfLIS(vec1) << endl;
// 4
// ---
int nums2[] = {4, 10, 4, 3, 8, 9};
vector<int> vec2(nums2, nums2 + sizeof(nums2)/sizeof(int));
cout << Solution().lengthOfLIS(vec2) << endl;
// 3
// ---
int nums3[] = {2, 2};
vector<int> vec3(nums3, nums3 + sizeof(nums3)/sizeof(int));
cout << Solution().lengthOfLIS(vec3) << endl;
// 1
// ---
int nums4[] = {1, 3, 6, 7, 9, 4, 10, 5, 6};
vector<int> vec4(nums4, nums4 + sizeof(nums4)/sizeof(int));
cout << Solution().lengthOfLIS(vec4) << endl;
// 6
return 0;
}