LongestContinuousIncreasingSubsequence   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
eloc 18
c 2
b 0
f 0
dl 0
loc 33
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A findLengthOfLCIS2() 0 14 4
A findLengthOfLCIS() 0 15 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
class LongestContinuousIncreasingSubsequence
8
{
9
    public static function findLengthOfLCIS(array $nums): int
10
    {
11
        if (empty($nums)) {
12
            return 0;
13
        }
14
        $res = $cnt = 0;
15
        for ($i = 0, $n = count($nums); $i < $n; $i++) {
16
            if ($i === 0 || $nums[$i] > $nums[$i - 1]) {
17
                $res = max($res, ++$cnt);
18
            } else {
19
                $cnt = 1;
20
            }
21
        }
22
23
        return $res;
24
    }
25
26
    public static function findLengthOfLCIS2(array $nums): int
27
    {
28
        if (empty($nums)) {
29
            return 0;
30
        }
31
        $n = count($nums);
32
        $dp = array_fill(0, $n, 1);
33
        for ($i = 1; $i < $n; $i++) {
34
            if ($nums[$i] > $nums[$i - 1]) {
35
                $dp[$i] = $dp[$i - 1] + 1;
36
            }
37
        }
38
39
        return max($dp);
40
    }
41
}
42