-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
152 lines (138 loc) · 3.03 KB
/
main.go
File metadata and controls
152 lines (138 loc) · 3.03 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Source: https://leetcode.com/problems/bomb-enemy
// Title: Bomb Enemy
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Given an `m x n` matrix `grid` where each cell is either a wall `'W'`, an enemy `'E'` or empty `'0'`, return the maximum enemies you can kill using one bomb. You can only place the bomb in an empty cell.
//
// The bomb kills all the enemies in the same row and column from the planted point until it hits the wall since it is too strong to be destroyed.
//
// **Example 1:**
// https://assets.leetcode.com/uploads/2021/03/27/bomb1-grid.jpg
//
// ```
// Input: grid = [["0","E","0","0"],["E","0","W","E"],["0","E","0","0"]]
// Output: 3
// ```
//
// **Example 2:**
// https://assets.leetcode.com/uploads/2021/03/27/bomb2-grid.jpg
//
// ```
// Input: grid = [["W","W","W"],["0","0","0"],["E","E","E"]]
// Output: 1
// ```
//
// **Constraints:**
//
// - `m == grid.length`
// - `n == grid[i].length`
// - `1 <= m, n <= 500`
// - `grid[i][j]` is either `'W'`, `'E'`, or `'0'`.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
func maxKilledEnemies(grid [][]byte) int {
m, n := len(grid), len(grid[0])
kills := make([][]int, m)
for i := range m {
kills[i] = make([]int, n)
}
for i := range m {
{ // from left to right
kill := 0
for j := 0; j < n; j++ {
switch grid[i][j] {
case '0':
kills[i][j] += kill
case 'E':
kill++
case 'W':
kill = 0
}
}
}
{ // from right to left
kill := 0
for j := n - 1; j >= 0; j-- {
switch grid[i][j] {
case '0':
kills[i][j] += kill
case 'E':
kill++
case 'W':
kill = 0
}
}
}
}
for j := range n {
{ // from top to bottom
kill := 0
for i := 0; i < m; i++ {
switch grid[i][j] {
case '0':
kills[i][j] += kill
case 'E':
kill++
case 'W':
kill = 0
}
}
}
{ // from bottom to top
kill := 0
for i := m - 1; i >= 0; i-- {
switch grid[i][j] {
case '0':
kills[i][j] += kill
case 'E':
kill++
case 'W':
kill = 0
}
}
}
}
// Find answer
ans := 0
for _, row := range kills {
for _, kill := range row {
ans = max(ans, kill)
}
}
return ans
}
func maxKilledEnemies2(grid [][]byte) int {
m, n := len(grid), len(grid[0])
ans := 0
rowKills := 0
colKills := make([]int, n)
for i := range m {
for j := range n {
// Recompute rowKills
if j == 0 || grid[i][j-1] == 'W' {
rowKills = 0
for k := j; k < n && grid[i][k] != 'W'; k++ {
if grid[i][k] == 'E' {
rowKills++
}
}
}
// Recompute colKills
if i == 0 || grid[i-1][j] == 'W' {
colKills[j] = 0
for k := i; k < m && grid[k][j] != 'W'; k++ {
if grid[k][j] == 'E' {
colKills[j]++
}
}
}
// Place bomb
if grid[i][j] == '0' {
ans = max(ans, rowKills+colKills[j])
}
}
}
return ans
}