-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlargestPlusSign.py
36 lines (30 loc) · 1.15 KB
/
largestPlusSign.py
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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/largest-plus-sign/
# Author: Miao Zhang
# Date: 2021-03-08
class Solution:
def orderOfLargestPlusSign(self, N: int, mines: List[List[int]]) -> int:
banned = {tuple(mine) for mine in mines}
dp = [[0 for _ in range(N)] for _ in range(N)]
res = 0
for i in range(N):
cnt = 0
for j in range(N): # left
cnt = 0 if (i, j) in banned else cnt + 1
dp[i][j] = cnt
cnt = 0
for j in range(N - 1, -1, -1): # right
cnt = 0 if (i, j) in banned else cnt + 1
dp[i][j] = min(dp[i][j], cnt)
for j in range(N):
cnt = 0
for i in range(N): # up
cnt = 0 if (i, j) in banned else cnt + 1
dp[i][j] = min(dp[i][j], cnt)
cnt = 0
for i in range(N - 1, -1, -1):
cnt = 0 if (i, j) in banned else cnt + 1
dp[i][j] = min(dp[i][j], cnt)
res = max(dp[i][j], res)
return res