Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Troubleshooting Longest Increasing Subsequence Algorithm

Pony2

Coder
Hello,

I'm having trouble troubleshooting a problem with the longest increasing subsequence algorithm. I'm using the code from the blog post, but I'm getting an incorrect output. Here's the code I'm using:

Code:
def lis(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

arr = [10 , 22 , 9 , 33 , 21 , 50 , 41 , 60]
print("Length of lis is", lis(arr))

The output I'm getting is 6, when it should be 5
Does anyone have any advice on how I can troubleshoot this?

Thanks in advance!
 
Actually, the output of this code is 5, which is correct. I don't understand how you can say you are getting 6.
But I notice you left out the last element (80) of the input array as used in the blog post. That would have given you 6.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom