-
Notifications
You must be signed in to change notification settings - Fork 584
Expand file tree
/
Copy pathLongest_Increasing_Subsequence.cpp
More file actions
83 lines (69 loc) · 2.26 KB
/
Longest_Increasing_Subsequence.cpp
File metadata and controls
83 lines (69 loc) · 2.26 KB
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
#include <vector>
#include <algorithm>
class Solution {
public:
// Optimal O(n log n) solution using binary search
int lengthOfLIS(const std::vector<int>& nums) {
if (nums.empty()) {
return 0;
}
std::vector<int> tails; // tails[i] = smallest tail of LIS with length i+1
for (int num : nums) {
// Binary search for position to insert/replace
auto it = std::lower_bound(tails.begin(), tails.end(), num);
if (it == tails.end()) {
tails.push_back(num); // Extend the sequence
} else {
*it = num; // Replace to keep smallest tail
}
}
return tails.size();
}
// Dynamic Programming O(n²) solution
int lengthOfLIS_DP(const std::vector<int>& nums) {
if (nums.empty()) {
return 0;
}
int n = nums.size();
std::vector<int> dp(n, 1); // dp[i] = length of LIS ending at i
int maxLen = 1;
for (int i = 1; i < n; ++i) {
for (int j = 0; j < i; ++j) {
if (nums[j] < nums[i]) {
dp[i] = std::max(dp[i], dp[j] + 1);
}
}
maxLen = std::max(maxLen, dp[i]);
}
return maxLen;
}
// Get actual LIS sequence (not just length)
std::vector<int> getLIS(const std::vector<int>& nums) {
if (nums.empty()) {
return {};
}
int n = nums.size();
std::vector<int> dp(n, 1);
std::vector<int> parent(n, -1);
int maxLen = 1, maxIdx = 0;
for (int i = 1; i < n; ++i) {
for (int j = 0; j < i; ++j) {
if (nums[j] < nums[i] && dp[j] + 1 > dp[i]) {
dp[i] = dp[j] + 1;
parent[i] = j;
}
}
if (dp[i] > maxLen) {
maxLen = dp[i];
maxIdx = i;
}
}
// Reconstruct LIS
std::vector<int> lis;
for (int i = maxIdx; i != -1; i = parent[i]) {
lis.push_back(nums[i]);
}
std::reverse(lis.begin(), lis.end());
return lis;
}
};