Skip to content

Commit

Permalink
Assing PHP implementation for LIS
Browse files Browse the repository at this point in the history
  • Loading branch information
sukritishah15 committed Mar 19, 2020
1 parent aae19d1 commit 8fbb2f9
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions Longest_Increasing_Subsequence/LIS.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
// PHP Implementation of LIS using Dynamic Programming

// fumction to return the length of LIS
function lisLen($arr, $n){
// $dp[$i] represents length of LIS ending at index i

// 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
*/

?>

0 comments on commit 8fbb2f9

Please sign in to comment.