Advertisement
kosievdmerwe

Untitled

Sep 9th, 2021
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.00 KB | None | 0 0
  1. TOP = 0
  2. RIGHT = 1
  3. BOTTOM = 2
  4. LEFT = 3
  5.  
  6. class Solution:
  7.     def orderOfLargestPlusSign(self, n: int, mines: List[List[int]]) -> int:
  8.         mines = set(map(tuple, mines))
  9.         # We have a sentry value and we make use of the fact that in Python
  10.         # the index -1 is the same as the last index in the array
  11.         grid = [[[0] * 4 for y in range(n + 1)] for x in range(n + 1)]
  12.        
  13.         for x in range(n):
  14.             for y in range(n):
  15.                 if (x, y) in mines:
  16.                     continue
  17.                 grid[x][y][LEFT] = grid[x-1][y][LEFT] + 1
  18.                 grid[x][y][TOP] = grid[x][y-1][TOP] + 1
  19.                
  20.         for x in range(n - 1, -1, -1):
  21.             for y in range(n - 1, -1, -1):
  22.                 if (x, y) in mines:
  23.                     continue
  24.                 grid[x][y][RIGHT] = grid[x+1][y][RIGHT] + 1
  25.                 grid[x][y][BOTTOM] = grid[x][y+1][BOTTOM] + 1
  26.        
  27.         return max(min(grid[x][y]) for x in range(n) for y in range(n))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement