forked from manthanghasadiya/Diff-Strings-operations
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongest Bitonic Subsequence.cpp
More file actions
95 lines (77 loc) · 2.1 KB
/
Longest Bitonic Subsequence.cpp
File metadata and controls
95 lines (77 loc) · 2.1 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
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <vector>
#include <list>
using namespace std;
// Function to find the longest bitonic subsequence in an array
void LBS(vector<int> const &nums)
{
int n = nums.size();
// base case
if (n == 0) {
return;
}
// `I[i]` store the longest increasing subsequence, ending at `nums[i]`
list<int> I[n];
I[0].push_back(nums[0]);
for (int i = 1; i < n; i++)
{
for (int j = 0; j < i; j++)
{
if (I[i].size() < I[j].size() && nums[i] > nums[j]) {
I[i] = I[j];
}
}
I[i].push_back(nums[i]);
}
// `D[i]` stores the longest decreasing subsequence, starting from `nums[i]`
list<int> D[n];
D[n - 1].push_front(nums[n - 1]);
for (int i = n - 2; i >= 0; i--)
{
for (int j = n - 1; j > i; j--)
{
if (D[i].size() < D[j].size() && nums[i] > nums[j]) {
D[i] = D[j];
}
}
D[i].push_front(nums[i]);
}
// uncomment the following code to print contents of vector LIS and LDS
/* for (int i = 0; i < n; i++)
{
cout << "LIS[" << i << "] — ";
for (int j: I[i]) {
cout << j << " ";
}
cout << "\t\tLDS[" << i << "] — ";
for (int j: D[i]) {
cout << j << " ";
}
cout << endl;
} */
// find the peak element index
int peak = 0;
for (int i = 1; i < n; i++)
{
if ((I[i].size() + D[i].size()) > (I[peak].size() + D[peak].size())) {
peak = i;
}
}
cout << "The longest bitonic subsequence is ";
// print longest increasing subsequence ending at peak element
for (int i: I[peak]) {
cout << i << " ";
}
// pop the front element of LDS as it points to the same element as the rear of LIS
D[peak].pop_front();
// print longest decreasing subsequence starting from the peak element
for (int i: D[peak]) {
cout << i << " ";
}
}
int main()
{
vector<int> nums = { 4, 2, 5, 9, 7, 6, 10, 3, 1 };
LBS(nums);
return 0;
}