Longest Increasing Subsequence

Find the length of the longest increasing subsequence in an array using dynamic programming.

Code

Algorithms
$n = count($arr);
$dp = array_fill(0, $n, 1);
for ($i = 1; $i < $n; $i++) {
    for ($j = 0; $j < $i; $j++) {
        if ($arr[$i] > $arr[$j]) {
            $dp[$i] = max($dp[$i], $dp[$j] + 1);
        }
    }
}
return max($dp);

Parameters

Array of numbers

Server

More PHP Snippets