Longest Increasing Subsequence in C++

This document provides a C++ program to find the longest increasing subsequence (LIS) in an array. The LIS is a subsequence of a given sequence where the subsequence’s elements are sorted in increasing order. This problem is commonly solved using dynamic programming.

Program Explanation

The following C++ program uses dynamic programming to find the longest increasing subsequence:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

/**
 * Function to find the length of the longest increasing subsequence.
 * @param nums Vector of integers representing the array.
 * @return Length of the longest increasing subsequence.
 */
int lengthOfLIS(const vector<int> &nums) {
    if (nums.empty()) return 0;

    vector<int> dp(nums.size(), 1);

    for (size_t i = 1; i < nums.size(); ++i) {
        for (size_t j = 0; j < i; ++j) {
            if (nums[i] > nums[j]) {
                dp[i] = max(dp[i], dp[j] + 1);
            }
        }
    }

    return *max_element(dp.begin(), dp.end());
}

/**
 * Main function to execute the program.
 */
int main() {
    vector<int> nums = {10, 9, 2, 5, 3, 7, 101, 18};

    int length = lengthOfLIS(nums);
    cout << "Length of the Longest Increasing Subsequence: " << length << endl;

    return 0;
}
    

Program Structure

  1. Include Headers: The program includes necessary headers such as <iostream> for input/output operations, <vector> for using the vector container, and <algorithm> for using standard algorithms like max_element.
  2. lengthOfLIS Function: This function computes the length of the longest increasing subsequence.
    • dp: A vector that stores the length of the longest increasing subsequence ending at each index.
    • The function iterates over each element and updates the dp vector based on previous values to ensure it captures the longest subsequence.
  3. Main Function: This function initializes an array, calls lengthOfLIS, and prints the result.

Explanation

The dynamic programming approach maintains a dp array where dp[i] represents the length of the longest increasing subsequence ending at index i. The algorithm iterates over each element and updates the dp values by checking all previous indices. The maximum value in the dp array at the end of the iteration gives the length of the longest increasing subsequence.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)