Thread: Troubleshooting Longest Increasing Subsequence

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Banned
    Join Date
    Oct 2022
    Posts
    5

    Troubleshooting Longest Increasing Subsequence

    I'm having a bit of difficulty trying to implement the longest increasing subsequence algorithm. I found a <<crapware site>> online that looks helpful, but I'm having trouble translating the pseudocode into code.


    I'm using Python, and I've written the following function:


    Code:
    def longest_increasing_subsequence(arr):
        n = len(arr)
        lis = [1]*n
        
        for i in range(1, n):
            for j in range(0, i):
                if arr[i] > arr[j] and lis[i] < lis[j] + 1 :
                    lis[i] = lis[j]+1
     
        maximum = 0
     
        for i in range(n):
            maximum = max(maximum, lis[i])
     
        return maximum


    When I try to run the code, I get the following error:


    Code:
    NameError: name 'arr' is not defined
    I'm not sure what I'm doing wrong. Any help would be appreciated!
    Thank you.
    Last edited by Salem; 04-18-2023 at 01:11 PM. Reason: Yet another spam post disguised as a question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generate all subsequence combinations
    By Freaky256 in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2015, 11:04 PM
  2. Replies: 1
    Last Post: 03-16-2012, 02:07 AM
  3. Max SubSequence using recursion
    By Amoxaphobic in forum C++ Programming
    Replies: 4
    Last Post: 11-10-2011, 02:46 PM
  4. Minimum Positive Subsequence Sum
    By CrazyNorman in forum C++ Programming
    Replies: 2
    Last Post: 09-11-2008, 04:25 AM
  5. Longest Common Subsequence
    By stimpyzu in forum Tech Board
    Replies: 4
    Last Post: 04-04-2005, 03:18 PM

Tags for this Thread