File tree Expand file tree Collapse file tree 2 files changed +47
-0
lines changed Expand file tree Collapse file tree 2 files changed +47
-0
lines changed Original file line number Diff line number Diff line change 349
349
358|[ Rearrange String k Distance Apart] ( ./solutions/0358-rearrange-string-k-distance-apart.js ) |Hard|
350
350
359|[ Logger Rate Limiter] ( ./solutions/0359-logger-rate-limiter.js ) |Easy|
351
351
360|[ Sort Transformed Array] ( ./solutions/0360-sort-transformed-array.js ) |Medium|
352
+ 361|[ Bomb Enemy] ( ./solutions/0361-bomb-enemy.js ) |Medium|
352
353
363|[ Max Sum of Rectangle No Larger Than K] ( ./solutions/0363-max-sum-of-rectangle-no-larger-than-k.js ) |Hard|
353
354
365|[ Water and Jug Problem] ( ./solutions/0365-water-and-jug-problem.js ) |Medium|
354
355
367|[ Valid Perfect Square] ( ./solutions/0367-valid-perfect-square.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 361. Bomb Enemy
3
+ * https://leetcode.com/problems/bomb-enemy/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given an m x n matrix grid where each cell is either a wall 'W', an enemy 'E' or empty '0',
7
+ * return the maximum enemies you can kill using one bomb. You can only place the bomb in an
8
+ * empty cell.
9
+ *
10
+ * The bomb kills all the enemies in the same row and column from the planted point until it
11
+ * hits the wall since it is too strong to be destroyed.
12
+ */
13
+
14
+ /**
15
+ * @param {character[][] } grid
16
+ * @return {number }
17
+ */
18
+ var maxKilledEnemies = function ( grid ) {
19
+ const rows = grid . length ;
20
+ const cols = grid [ 0 ] . length ;
21
+ const colHits = new Array ( cols ) . fill ( 0 ) ;
22
+ let rowHits = 0 ;
23
+ let result = 0 ;
24
+
25
+ for ( let i = 0 ; i < rows ; i ++ ) {
26
+ for ( let j = 0 ; j < cols ; j ++ ) {
27
+ if ( j === 0 || grid [ i ] [ j - 1 ] === 'W' ) {
28
+ rowHits = 0 ;
29
+ for ( let k = j ; k < cols && grid [ i ] [ k ] !== 'W' ; k ++ ) {
30
+ if ( grid [ i ] [ k ] === 'E' ) rowHits ++ ;
31
+ }
32
+ }
33
+ if ( i === 0 || grid [ i - 1 ] [ j ] === 'W' ) {
34
+ colHits [ j ] = 0 ;
35
+ for ( let k = i ; k < rows && grid [ k ] [ j ] !== 'W' ; k ++ ) {
36
+ if ( grid [ k ] [ j ] === 'E' ) colHits [ j ] ++ ;
37
+ }
38
+ }
39
+ if ( grid [ i ] [ j ] === '0' ) {
40
+ result = Math . max ( result , rowHits + colHits [ j ] ) ;
41
+ }
42
+ }
43
+ }
44
+
45
+ return result ;
46
+ } ;
You can’t perform that action at this time.
0 commit comments