mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-11-28 23:15:33 +08:00
formatting source-code for 153fb7b8a5
This commit is contained in:
@@ -5,24 +5,19 @@
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
int LIS(int arr[], int n)
|
||||
{
|
||||
int LIS(int arr[], int n) {
|
||||
set<int> active; // The current built LIS.
|
||||
active.insert(arr[0]);
|
||||
// Loop through every element.
|
||||
for (int i = 1; i < n; ++i)
|
||||
{
|
||||
for (int i = 1; i < n; ++i) {
|
||||
auto get = active.lower_bound(arr[i]);
|
||||
if (get == active.end())
|
||||
{
|
||||
if (get == active.end()) {
|
||||
active.insert(arr[i]);
|
||||
} // current element is the greatest so LIS increases by 1.
|
||||
else
|
||||
{
|
||||
else {
|
||||
int val = *get; // we find the position where arr[i] will be in the
|
||||
// LIS. If it is in the LIS already we do nothing
|
||||
if (val > arr[i])
|
||||
{
|
||||
if (val > arr[i]) {
|
||||
// else we remove the bigger element and add a smaller element
|
||||
// (which is arr[i]) and continue;
|
||||
active.erase(get);
|
||||
@@ -32,15 +27,13 @@ int LIS(int arr[], int n)
|
||||
}
|
||||
return active.size(); // size of the LIS.
|
||||
}
|
||||
int main(int argc, char const* argv[])
|
||||
{
|
||||
int main(int argc, char const* argv[]) {
|
||||
int n;
|
||||
cout << "Enter size of array: ";
|
||||
cin >> n;
|
||||
int a[n];
|
||||
cout << "Enter array elements: ";
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
for (int i = 0; i < n; ++i) {
|
||||
cin >> a[i];
|
||||
}
|
||||
cout << LIS(a, n) << endl;
|
||||
|
||||
Reference in New Issue
Block a user