Test Failed
Push — master ( 85122a...6cf2a8 )
by Jinyun
10:32
created

CellsWithOddValueInAMatrix   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
eloc 22
c 1
b 0
f 0
dl 0
loc 41
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B oddCells2() 0 17 8
B oddCells() 0 20 8
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
class CellsWithOddValueInAMatrix
8
{
9
    public static function oddCells(int $m, int $n, array $indices): int
10
    {
11
        if ($m <= 0 || $n <= 0 || empty($indices)) {
12
            return 0;
13
        }
14
15
        [$cnt, $row, $col] = [0, array_fill(0, $m, 0), array_fill(0, $n, 0)];
16
        foreach ($indices as $item) {
17
            $row[$item[0]]++;
18
            $col[$item[1]]++;
19
        }
20
        for ($i = 0; $i < $m; $i++) {
21
            for ($j = 0; $j < $n; $j++) {
22
                if (($row[$i] + $col[$j]) % 2 !== 0) {
23
                    $cnt++;
24
                }
25
            }
26
        }
27
28
        return $cnt;
29
    }
30
31
    public static function oddCells2(int $m, int $n, array $indices): int
32
    {
33
        if ($m <= 0 || $n <= 0 || empty($indices)) {
34
            return 0;
35
        }
36
        [$cnt, $row, $col] = [0, array_fill(0, $m, false), array_fill(0, $n, false)];
37
        foreach ($indices as $item) {
38
            $row[$item[0]] ^= true;
39
            $col[$item[1]] ^= true;
40
        }
41
        for ($i = 0; $i < $m; $i++) {
42
            for ($j = 0; $j < $n; $j++) {
43
                $cnt += $row[$i] ^ $col[$j] ? 1 : 0;
44
            }
45
        }
46
47
        return $cnt;
48
    }
49
}
50