-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBombEnemy.php
78 lines (70 loc) · 2.62 KB
/
BombEnemy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
declare(strict_types=1);
namespace leetcode;
class BombEnemy
{
public static function maxKilledEnemies(array $grids): int
{
[$res, $m, $n] = [0, count($grids), count($grids[0])];
if ($m <= 0 || $n <= 0) {
return $res;
}
$v1 = $v2 = $v3 = $v4 = array_fill(0, $m, array_fill(0, $n, 0));
for ($i = 0; $i < $m; $i++) {
for ($j = 0; $j < $n; $j++) { // 列从上到下
$t = ($j === 0 || $grids[$i][$j] === 'W') ? 0 : $v1[$i][$j - 1];
$v1[$i][$j] = $grids[$i][$j] === 'E' ? $t + 1 : $t;
}
for ($j = $n - 1; $j >= 0; $j--) { // 列从下到上
$t = ($j === $n - 1 || $grids[$i][$j] === 'W') ? 0 : $v2[$i][$j + 1];
$v2[$i][$j] = $grids[$i][$j] === 'E' ? $t + 1 : $t;
}
}
for ($j = 0; $j < $n; $j++) {
for ($i = 0; $i < $m; $i++) { // 行从左到右
$t = ($i === 0 || $grids[$i][$j] === 'W') ? 0 : $v3[$i - 1][$j];
$v3[$i][$j] = $grids[$i][$j] === 'E' ? $t + 1 : $t;
}
for ($i = $m - 1; $i >= 0; $i--) { // 行从右到左
$t = ($i === $m - 1 || $grids[$i][$j] === 'W') ? 0 : $v4[$i + 1][$j];
$v4[$i][$j] = $grids[$i][$j] === 'E' ? $t + 1 : $t;
}
}
for ($i = 0; $i < $m; $i++) {
for ($j = 0; $j < $n; $j++) {
if ($grids[$i][$j] === '0') {
$res = max($res, $v1[$i][$j] + $v2[$i][$j] + $v3[$i][$j] + $v4[$i][$j]);
}
}
}
return $res;
}
public static function maxKilledEnemies2(array $grids): int
{
[$res, $m, $n] = [0, count($grids), count($grids[0])];
if ($m <= 0 || $n <= 0) {
return $res;
}
$col = array_fill(0, $n, 0);
for ($i = 0; $i < $m; $i++) {
for ($j = 0; $j < $n; $j++) {
if ($j === 0 || $grids[$i][$j - 1] === 'W') {
$row = 0;
for ($k = $j; $k < $n && $grids[$i][$k] !== 'W'; $k++) {
$row += $grids[$i][$k] === 'E';
}
}
if ($i === 0 || $grids[$i - 1][$j] === 'W') {
$col[$j] = 0;
for ($k = $i; $k < $m && $grids[$k][$j] !== 'W'; $k++) {
$col[$j] += $grids[$k][$j] === 'E';
}
}
if ($grids[$i][$j] === '0') {
$res = max($res, $row + $col[$j]);
}
}
}
return $res;
}
}