473,473 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Bug in code of Longest Increasing Subsequence problem

1 New Member
Longest Increasing Subsequence
The Longest Increasing Subsequence problem is to find a subsequence of a given sequence in which the subsequence's elements are in sorted order and in which the subsequence is as long as possible.
I'm trying to implement a program for Longest Increasing Subsequence, It's giving correct output for some input patterns, But for some it's giving incorrect result. Can anybody tell me where is the bug in the code.
Here's my code

Expand|Select|Wrap|Line Numbers
  1. def binary_search(t, l, r, key):
  2.     while (r - l > 1):
  3.         m = l + (r - l)//2
  4.         if (t[m]>=key):
  5.             r = m
  6.         else:
  7.             l = m
  8.     return r
  9.  
  10. def LIS():
  11.     tailTable = [0] * N
  12.     tailTable[0] = lst[0]
  13.     len = 1
  14.     for i in range(1, N):
  15.         if (lst[i] < tailTable[0]):
  16.             # New Smallest Value
  17.             tailTable[0] = lst[i]
  18.         elif (lst[i] > tailTable[len - 1]):
  19.             # lst[i] wants to extend largest subsequence
  20.             tailTable[len] = lst[i]
  21.             len += 1
  22.         else:
  23.             # A[i] wants to be current end candidate of an existing
  24.             # subsequence.
  25.             tailTable[binary_search(tailTable, -1, len-1, lst[i])] = lst[i]
  26.  
  27.     return len
  28.  
  29. if __name__ == "__main__":
  30.     N = int(input())
  31.  
  32.     lst = []
  33.     for i in range(N):
  34.         lst.append(input())
  35.     ret = LIS()
  36.     print(ret)
Jul 4 '16 #1
0 888

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: SpookyET | last post by:
I want to create a wrapper for SQLite and I'm having a problem with getting NullReferenceException when calling the unmanaged function. I do not wish to write in C++ since i do not have enough...
1
by: cybertof | last post by:
Hello, Could you please tell me what's wrong with this code (c# office code behind) : protected void ThisWorkbook_Open() { try {
0
by: charles_weaver | last post by:
I create a Code Comment Web Report for a C# ( or C++) project. All of the files are there, one for each method. But when I run the meain page it doesn't seem to hook together. I get to the page...
1
by: Gabriël | last post by:
Hi All, I've created an eventhandler for ItemdataBound and ItemCreated, but when I access "e.Item.Cells.Text" it's always empty. BUT, when I assign a value to this empty field (while being in...
14
by: John Salerno | last post by:
Specifically, I'm using UltraEdit and perhaps there's no way perfect way to implement code folding with it, given how it uses its syntax highlighting file to do so (i.e., you have to specify an...
0
by: alunharford | last post by:
I'm writing an application that is trusted, but I want it to run some untrusted code, and I don't understand how I do that. I'm including an example. I want to trust my class, TrustedClass, to do...
4
by: NoDBExperience | last post by:
I have a situation with my DB. I added a couple of columns to a certain table, which apparently affected many forms, rendering them inoperable. Therfore, i would like to know if there is any code out...
1
by: beardog | last post by:
I've got a question for the gurus out there. I'm trying to create an IF conditional inside my aspx page that reads the state of a boolean in the code behind. Here's the basic ASP: ...
4
by: soule | last post by:
Hi, Everyone, I'm trying to adapt code for an .accdb form button in the main form class module that moves a piece of data from a field in one table to the same named field in another table. Not...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.