1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
grid-illumination.py 1.33 KB
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 6年前 . Create grid-illumination.py
# Time: O(l + q)
# Space: O(l)
import collections
class Solution(object):
def gridIllumination(self, N, lamps, queries):
"""
:type N: int
:type lamps: List[List[int]]
:type queries: List[List[int]]
:rtype: List[int]
"""
directions = [(0, -1), (0, 1), (-1, 0), (1, 0),
(-1, -1), (1, -1), (1, -1), (1, 1)]
lookup = set()
row = collections.defaultdict(int)
col = collections.defaultdict(int)
diag = collections.defaultdict(int)
anti = collections.defaultdict(int)
for r, c in lamps:
lookup.add((r, c))
row[r] += 1
col[c] += 1
diag[r-c] += 1
anti[r+c] += 1
result = []
for r, c in queries:
if row[r] or col[c] or \
diag[r-c] or anti[r+c]:
result.append(1)
else:
result.append(0)
for d in directions:
nc, nr = r+d[0], c+d[1]
if not (0 <= nr < N and 0 <= nc < N and \
(nr, nc) in lookup):
continue
lookup.remove((nr, nc))
row[nr] -= 1
col[nc] -= 1
diag[nr-nc] -= 1
anti[nr+nc] -= 1
return result
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助