dynamic programming - Explain the algorithm to solve 'longest increasing subsequence' problem -


I have been trying to understand this algorithm for the last 2 hours, but I can not get it. Can anyone explain it easily?

  function lis_length (a) n: = a.length q: = 0 for n for new array (n): max: = 0; 0 to k for j, if any [k] & gt; A [ja]: if q [j] & gt; Maximum, then Maximum = q [j] Set Q [K]: = Max + 1; For maximum: = 0, I am 0 to n: If q [i] & gt; Maximum, then Maximum = q [i] Set Maximum Return; After the first double (double) loop,  q [i]    

status < />>

already ends in the status of j The largest growing follow-up length, but between j and 0 and k-1 . Given this, how do you calculate q [k] ?

Good, you can type j with j and a [j] & lt; One of the [k] , related q [j] values ​​is the largest, add one, and hide that value in q [k] See for This is what internal loops do.

Then in the entry in the internal loop, q [j] is already the correct value for 0 and k-1 Click . And on the exit, there is also the right value for k , so when a double loop leaves, between q [i] in all i 0 and n .

The final loop makes the largest selection of those people, which is the answer.

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

ruby on rails - Convert a Activerecord result with has_one associations into a single hash per record -

javascript - Read complete data from stream from onMessage -