Skip to content

Instantly share code, notes, and snippets.

@adrianosferreira
Created December 13, 2019 15:12
Show Gist options
  • Save adrianosferreira/5b15a66af6484a78817cf650e7484061 to your computer and use it in GitHub Desktop.
Save adrianosferreira/5b15a66af6484a78817cf650e7484061 to your computer and use it in GitHub Desktop.
List in O(N^2) with PHP
<?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