Ctrl K

Best Spot for the Turret

Medium
The game board is an m × n table grid. Each cell holds one character: 'W' is a wall, 'E' is a target, and '0' is an empty cell. You may place a laser turret in one empty cell. It fires in all four directions at once: up, down, left and right. A beam passes through targets, hitting every one on its way, but stops at the first wall (which it cannot destroy) or at the edge of the board. Return the largest number of targets a single shot can hit. If there is no empty cell, return 0.
Example 1
grid=[["E", "0", "E"], ["0", "W", "E"], ["E", "0", "0"]] → 3

A turret in the bottom-right corner (row 2, column 2) hits the two targets above it in the last column and the target at the start of its row: 3 in total.

Example 2
grid=[["E", "W", "0", "E"]] → 1

The only empty cell sees the target to its right; the one on the left is behind a wall.

Example 3
grid=[["E", "E"], ["W", "E"]] → 0

There are no empty cells, so the turret cannot be placed and the answer is 0.

Constraints
  • 1 ≤ m, n ≤ 500
  • All rows of grid have the same length n
  • grid[i][j] is one of 'W', 'E', '0'
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsWarehouse Cart Routes MediumCheapest Corner-to-Corner Route MediumLargest Solid Square MediumSteps to the Nearest Zero MediumLongest Lit Line Medium
loading Python…
Input
grid=[["E", "0", "E"], ["0", "W", "E"], ["E", "0", "0"]]
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 71 tests on the server, hidden ones included.