Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Dynamic Programming Challenge: Longest Increasing Subsequence
Forum Updated to NodeBB v4.3 + New Features

Dynamic Programming Challenge: Longest Increasing Subsequence

Scheduled Pinned Locked Moved Unsolved C++ Gurus
2 Posts 2 Posters 627 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sachin Bhatt
    wrote on 15 Dec 2023, 07:21 last edited by
    #1

    Given an array of integers, find the length of the longest increasing subsequence. A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.

    For example:

    #include <iostream>
    #include <vector>
    
    int longestIncreasingSubsequence(const std::vector<int>& nums) {
        // Your dynamic programming solution goes here.
    
        // Return the length of the longest increasing subsequence.
    }
    
    int main() {
        std::vector<int> sequence = {10, 22, 9, 33, 21, 50, 41, 60, 80};
        int result = longestIncreasingSubsequence(sequence);
    
        std::cout << "Length of the longest increasing subsequence: " << result << std::endl;
    
        return 0;
    }
    
    

    I'm specifically interested in implementing this using dynamic programming techniques. How can I approach this problem using dynamic programming, and what would be the C++ code for solving it? Any insights, code snippets, or explanations would be incredibly helpful in mastering dynamic programming for this particular challenge. Thank you for your assistance!

    J 1 Reply Last reply 15 Dec 2023, 08:17
    0
    • S Sachin Bhatt
      15 Dec 2023, 07:21

      Given an array of integers, find the length of the longest increasing subsequence. A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.

      For example:

      #include <iostream>
      #include <vector>
      
      int longestIncreasingSubsequence(const std::vector<int>& nums) {
          // Your dynamic programming solution goes here.
      
          // Return the length of the longest increasing subsequence.
      }
      
      int main() {
          std::vector<int> sequence = {10, 22, 9, 33, 21, 50, 41, 60, 80};
          int result = longestIncreasingSubsequence(sequence);
      
          std::cout << "Length of the longest increasing subsequence: " << result << std::endl;
      
          return 0;
      }
      
      

      I'm specifically interested in implementing this using dynamic programming techniques. How can I approach this problem using dynamic programming, and what would be the C++ code for solving it? Any insights, code snippets, or explanations would be incredibly helpful in mastering dynamic programming for this particular challenge. Thank you for your assistance!

      J Offline
      J Offline
      JonB
      wrote on 15 Dec 2023, 08:17 last edited by JonB
      #2

      @Sachin-Bhatt
      I had to look up what "dynamic programming" is! They like their labels for things, don't they?

      Am I missing something, or is this real easy? You start by setting a variable for the longest subsequence to 0. You count from left to right while the numbers increase. So for 10, 22 and then 9 the longest so far is 2 (10, 22), terminated by 9 (because it is less than the preceding 22). You note that 2 is the longest so far, because it is more than 0, and so update the count variable. You know that you can resume looking from the 9. You encounter 9, 33 and then 21, 50, both terminated by smaller numbers, and those are also of length 2, no better then before. Finally you meet 41, 60, 80, terminated by end of input, that is length 3, and so is the highest. So that is what you return....

      1 Reply Last reply
      0

      2/2

      15 Dec 2023, 08:17

      • Login

      • Login or register to search.
      2 out of 2
      • First post
        2/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved