forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLIS.php
43 lines (33 loc) · 934 Bytes
/
LIS.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
// PHP Implementation of LIS using Dynamic Programming
// fumction to return the length of LIS
function lisLen($arr, $n){
// length of LIS ending at index 0 is 1
$dp[0] = 1;
// compute the length of LIS ending at each index
for ($i = 1; $i < $n; $i++){
$dp[$i] = 1;
for ($j = 0; $j < $i; $j++)
if ($arr[$i] > $arr[$j] && $dp[$i] < $dp[$j] + 1)
$dp[$i] = $dp[$j] + 1;
}
// find the max out of computed lengths of LIS
// ending at different indices
$max = PHP_INT_MIN;
for($i = 0; $i < $n; $i++){
if($max < $dp[$i])
$max = $dp[$i];
}
return $max;
}
// Sample test case
$arr = array(3, 10, 2, 1, 20, 40, 60, 7);
$n = sizeof($arr);
echo "Length of LIS is " , lisLen($arr, $n);
/*
Sample Input as specified in the code:
3, 10, 2, 1, 20, 40, 60, 7
Sample Output:
Length of LIS is 5
*/
?>