Skip to content

Commit 554919d

Browse files
Panquesito7manncodesgithub-actions
authored
[feat/fix/docs]: Improve the dynamic_programming/longest_increasing_subsequence.cpp file (TheAlgorithms#1504)
* [feat/fix/docs]: Improve the `dynamic_programming/longest_increasing_subsequence.cpp` file * [test/feat]: Add self-test implementations and... ...namespace (`dynamic_programming`). Thanks to @manncodes for the idea and help! Co-authored-by: Mann Patel <manncodes@users.noreply.github.com> * clang-format and clang-tidy fixes for 7d4562d Co-authored-by: Mann Patel <manncodes@users.noreply.github.com> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 17405a0 commit 554919d

File tree

1 file changed

+81
-15
lines changed

1 file changed

+81
-15
lines changed
Lines changed: 81 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,98 @@
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);
641
for (int i = 0; i < n; ++i) {
742
lis[i] = 1;
843
}
944
for (int i = 0; i < n; ++i) {
1045
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) {
1247
lis[i] = lis[j] + 1;
48+
}
1349
}
1450
}
1551
int res = 0;
1652
for (int i = 0; i < n; ++i) {
17-
res = max(res, lis[i]);
53+
res = std::max(res, lis[i]);
1854
}
1955
return res;
2056
}
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+
*/
2180
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: ";
2789
for (int i = 0; i < n; ++i) {
28-
cin >> a[i];
90+
std::cin >> a[i];
2991
}
30-
cout << LIS(a, n) << endl;
92+
93+
std::cout << "\nThe result is: " << dynamic_programming::LIS(a, n)
94+
<< std::endl;
95+
test(); // run self-test implementations
96+
3197
return 0;
32-
}
98+
}

0 commit comments

Comments
 (0)