Test Failed
Push — master ( ee878d...3c65e9 )
by Jinyun
03:13
created

DietPlanPerformanceEasy::dietPlanPerformance()   A

Complexity

Conditions 6
Paths 11

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 21
rs 9.2222
cc 6
nc 11
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
class DietPlanPerformanceEasy
8
{
9
    public static function dietPlanPerformance(array $calories, int $k, int $lower, int $upper): int
10
    {
11
        if (empty($calories)) {
12
            return 0;
13
        }
14
        $res = $sum = 0;
15
        for ($i = 0; $i < $k - 1; $i++) {
16
            $sum += $calories[$i];
17
        }
18
        for ($i = $k - 1, $n = count($calories); $i < $n; $i++) {
19
            $sum += $calories[$i];
20
            if ($sum < $lower) {
21
                $res--;
22
            }
23
            if ($sum > $upper) {
24
                $res++;
25
            }
26
            $sum -= $calories[$i - $k + 1];
27
        }
28
29
        return $res;
30
    }
31
}
32