11
11
* @author [David Leal](https://github.com/Panquesito7)
12
12
*/
13
13
14
+ #include < cassert> // / for assert
14
15
#include < iostream> // / for IO operations
15
16
#include < climits> // / for std::max
16
17
#include < vector> // / for std::vector
17
18
19
+ /* *
20
+ * @namespace dynamic_programming
21
+ * @brief Dynamic Programming algorithms
22
+ */
23
+ namespace dynamic_programming {
18
24
/* *
19
25
* @brief Calculate the longest increasing subsequence for the specified numbers
20
26
* @param a the array used to calculate the longest increasing subsequence
21
27
* @param n the size used for the arrays
22
28
* @returns the length of the longest increasing
23
29
* subsequence in the `a` array of size `n`
24
30
*/
25
- int LIS (const std::vector<uint64_t > &a, const uint32_t &n) {
31
+ uint64_t LIS (const std::vector<uint64_t > &a, const uint32_t &n) {
26
32
std::vector<int > lis (n);
27
33
for (int i = 0 ; i < n; ++i) {
28
34
lis[i] = 1 ;
@@ -39,6 +45,21 @@ int LIS(const std::vector<uint64_t> &a, const uint32_t &n) {
39
45
}
40
46
return res;
41
47
}
48
+ } // namespace dynamic_programming
49
+
50
+ /* *
51
+ * @brief Self-test implementations
52
+ * @returns void
53
+ */
54
+ static void test (){
55
+ std::vector<uint64_t > a = {15 ,21 ,2 ,3 ,4 ,5 ,8 ,4 ,1 ,1 };
56
+ uint32_t n = a.size ();
57
+
58
+ uint32_t result = dynamic_programming::LIS (a, n);
59
+ assert (result == 5 ); // /< The longest increasing subsequence is `{2,3,4,5,8}`
60
+
61
+ std::cout << " Self-test implementations passed!" << std::endl;
62
+ }
42
63
43
64
/* *
44
65
* @brief Main function
@@ -59,6 +80,8 @@ int main(int argc, char const *argv[]) {
59
80
std::cin >> a[i];
60
81
}
61
82
62
- std::cout << " The result is: " << LIS (a, n) << std::endl;
83
+ std::cout << " \n The result is: " << dynamic_programming::LIS (a, n) << std::endl;
84
+ test (); // run self-test implementations
85
+
63
86
return 0 ;
64
87
}
0 commit comments