DistributeCandies   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 15
c 2
b 0
f 0
dl 0
loc 29
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A distributeCandies2() 0 11 3
A distributeCandies() 0 14 5
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