Test Failed
Push — master ( efa2c4...4ea105 )
by Jinyun
02:31
created

DistributeCandies::distributeCandies3()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 13
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
namespace leetcode;
4
5
class DistributeCandies
6
{
7
    public static function distributeCandies(array $candies): int
8
    {
9
        if (empty($candies)) {
10
            return 0;
11
        }
12
        sort($candies);
13
        [$cnt, $len] = [1, count($candies)];
14
        for ($i = 1; $i < $len && $cnt < $len / 2; $i++) {
15
            if ($candies[$i] > $candies[$i - 1]) {
16
                $cnt++;
17
            }
18
        }
19
20
        return $cnt;
21
    }
22
23
    public static function distributeCandies2(array $candies): int
24
    {
25
        if (empty($candies)) {
26
            return 0;
27
        }
28
        [$map, $len] = [[], count($candies)];
29
        foreach ($candies as $candy) {
30
            $map[$candy] = true;
31
        }
32
33
        return min(count($map), $len / 2);
34
    }
35
}
36