1
- // Program to calculate length of longest increasing subsequence in an array
2
- #include < bits/stdc++.h>
3
- using namespace std ;
4
- int LIS (int a[], int n) {
5
- int lis[n];
1
+ /* *
2
+ * @file
3
+ * @brief Calculate the length of the [longest increasing
4
+ * subsequence](https://en.wikipedia.org/wiki/Longest_increasing_subsequence) in
5
+ * an array
6
+ *
7
+ * @details
8
+ * In computer science, the longest increasing subsequence problem is to find a
9
+ * subsequence of a given sequence in which the subsequence's elements are in
10
+ * sorted order, lowest to highest, and in which the subsequence is as long as
11
+ * possible. This subsequence is not necessarily contiguous, or unique. Longest
12
+ * increasing subsequences are studied in the context of various disciplines
13
+ * related to mathematics, including algorithmics, random matrix theory,
14
+ * representation theory, and physics. The longest increasing subsequence
15
+ * problem is solvable in time O(n log n), where n denotes the length of the
16
+ * input sequence.
17
+ *
18
+ * @author [Krishna Vedala](https://github.com/kvedala)
19
+ * @author [David Leal](https://github.com/Panquesito7)
20
+ */
21
+
22
+ #include < cassert> // / for assert
23
+ #include < climits> // / for std::max
24
+ #include < iostream> // / for IO operations
25
+ #include < vector> // / for std::vector
26
+
27
+ /* *
28
+ * @namespace dynamic_programming
29
+ * @brief Dynamic Programming algorithms
30
+ */
31
+ namespace dynamic_programming {
32
+ /* *
33
+ * @brief Calculate the longest increasing subsequence for the specified numbers
34
+ * @param a the array used to calculate the longest increasing subsequence
35
+ * @param n the size used for the arrays
36
+ * @returns the length of the longest increasing
37
+ * subsequence in the `a` array of size `n`
38
+ */
39
+ uint64_t LIS (const std::vector<uint64_t > &a, const uint32_t &n) {
40
+ std::vector<int > lis (n);
6
41
for (int i = 0 ; i < n; ++i) {
7
42
lis[i] = 1 ;
8
43
}
9
44
for (int i = 0 ; i < n; ++i) {
10
45
for (int j = 0 ; j < i; ++j) {
11
- if (a[i] > a[j] && lis[i] < lis[j] + 1 )
46
+ if (a[i] > a[j] && lis[i] < lis[j] + 1 ) {
12
47
lis[i] = lis[j] + 1 ;
48
+ }
13
49
}
14
50
}
15
51
int res = 0 ;
16
52
for (int i = 0 ; i < n; ++i) {
17
- res = max (res, lis[i]);
53
+ res = std:: max (res, lis[i]);
18
54
}
19
55
return res;
20
56
}
57
+ } // namespace dynamic_programming
58
+
59
+ /* *
60
+ * @brief Self-test implementations
61
+ * @returns void
62
+ */
63
+ static void test () {
64
+ std::vector<uint64_t > a = {15 , 21 , 2 , 3 , 4 , 5 , 8 , 4 , 1 , 1 };
65
+ uint32_t n = a.size ();
66
+
67
+ uint32_t result = dynamic_programming::LIS (a, n);
68
+ assert (result ==
69
+ 5 ); // /< The longest increasing subsequence is `{2,3,4,5,8}`
70
+
71
+ std::cout << " Self-test implementations passed!" << std::endl;
72
+ }
73
+
74
+ /* *
75
+ * @brief Main function
76
+ * @param argc commandline argument count (ignored)
77
+ * @param argv commandline array of arguments (ignored)
78
+ * @returns 0 on exit
79
+ */
21
80
int main (int argc, char const *argv[]) {
22
- int n;
23
- cout << " Enter size of array: " ;
24
- cin >> n;
25
- int a[n];
26
- cout << " Enter array elements: " ;
81
+ uint32_t n = 0 ;
82
+
83
+ std::cout << " Enter size of array: " ;
84
+ std::cin >> n;
85
+
86
+ std::vector<uint64_t > a (n);
87
+
88
+ std::cout << " Enter array elements: " ;
27
89
for (int i = 0 ; i < n; ++i) {
28
- cin >> a[i];
90
+ std:: cin >> a[i];
29
91
}
30
- cout << LIS (a, n) << endl;
92
+
93
+ std::cout << " \n The result is: " << dynamic_programming::LIS (a, n)
94
+ << std::endl;
95
+ test (); // run self-test implementations
96
+
31
97
return 0 ;
32
- }
98
+ }
0 commit comments