Created
December 13, 2019 15:12
-
-
Save adrianosferreira/5b15a66af6484a78817cf650e7484061 to your computer and use it in GitHub Desktop.
List in O(N^2) with PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Solution { | |
/** | |
* @param Integer[] $nums | |
* @return Integer | |
*/ | |
function lengthOfLIS($arr) { | |
if ( ! $arr ) { | |
return 0; | |
} | |
if ( count($arr) === 1 ) { | |
return 1; | |
} | |
$lis = array_map( function(){ return 1; }, $arr ); | |
$lis = array_combine( array_keys( $arr ), $lis ); | |
$i = 0; | |
$j = 1; | |
$max = 1; | |
while ( $j < count( $arr ) && $i < count( $arr ) - 1 ) { | |
if ( $arr[ $j ] > $arr[ $i ] ) { | |
$new = $lis[ $i ] + 1; | |
if ( $new > $lis[ $j ] ) { | |
$lis[ $j ] = $lis[ $i ] + 1; | |
if ( ! $max || $max < $lis[ $i ] + 1 ) { | |
$max = $lis[ $i ] + 1; | |
} | |
} | |
} | |
if ( $i === $j - 1 ) { | |
$i = 0; | |
$j ++; | |
} else { | |
$i ++; | |
} | |
} | |
return $max; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment