题目如下:

On a N x N grid of cells, each cell (x, y) with 0 <= x < N and 0 <= y < N has a lamp.

Initially, some number of lamps are on.  lamps[i] tells us the location of the i-th lamp that is on.  Each lamp that is on illuminates every square on its x-axis, y-axis, and both diagonals (similar to a Queen in chess).

For the i-th query queries[i] = (x, y), the answer to the query is 1 if the cell (x, y) is illuminated, else 0.

After each query (x, y) [in the order given by queries], we turn off any lamps that are at cell (x, y) or are adjacent 8-directionally (ie., share a corner or edge with cell (x, y).)

Return an array of answers.  Each value answer[i]should be equal to the answer of the i-th query queries[i].

Example 1:

Input: N = 5, lamps = [[0,0],[4,4]], queries = [[1,1],[1,0]]
Output: [1,0]
Explanation:
Before performing the first query we have both lamps [0,0] and [4,4] on.
The grid representing which cells are lit looks like this, where [0,0] is the top left corner, and [4,4] is the bottom right corner:
1 1 1 1 1
1 1 0 0 1
1 0 1 0 1
1 0 0 1 1
1 1 1 1 1
Then the query at [1, 1] returns 1 because the cell is lit. After this query, the lamp at [0, 0] turns off, and the grid now looks like this:
1 0 0 0 1
0 1 0 0 1
0 0 1 0 1
0 0 0 1 1
1 1 1 1 1
Before performing the second query we have only the lamp [4,4] on. Now the query at [1,0] returns 0, because the cell is no longer lit.

Note:

  1. 1 <= N <= 10^9
  2. 0 <= lamps.length <= 20000
  3. 0 <= queries.length <= 20000
  4. lamps[i].length == queries[i].length == 2

解题思路:每一盏灯可以照亮所在位置的水平方向、垂直方向和两个对角线方向 ,假设这盏灯的坐标是(i,j),那么用一次函数来表示这水平和垂直方向就是x=i,y=j。两个对角线方向很显然斜率分别是1和-1,把(i,j)分别带入方程 y=x+b和y=-x+b即可求出b的值,而(斜率,b)这两个参数即可确定一条直线。这里可以用三个字典分别保存这四个方程,例如dic_x[i] ,dic_y[j], dic[(-1,b)],dic[(1,b)],每亮一盏灯,算出key值并对字典中相应的key所对应的值+1,而灭灯就是对key所对应的值减去1。判断一盏灯是否是亮的状态,算出四个方向的key值并判断这四个key中至少有一个存在于字典中即可。

代码如下:

class Solution(object):
dic = {}
dic_x = {}
dic_y = {}
dic_lamp = {}
def turn_on(self,x,y):
b = y - x
self.dic[(1, b)] = self.dic.setdefault((1, b), 0) + 1
b = x + y
self.dic[(-1, b)] = self.dic.setdefault((-1, b), 0) + 1 self.dic_x[x] = self.dic_x.setdefault(x, 0) + 1
self.dic_y[y] = self.dic_y.setdefault(y, 0) + 1
def turn_off(self,x,y):
b = y - x
if (1,b) in self.dic:
self.dic[(1, b)] -= 1
self.dic[(1, b)] = max(0,self.dic[(1, b)])
b = x + y
if (-1,b) in self.dic:
self.dic[(-1, b)] -= 1
if self.dic[(-1, b)] == 0:
del self.dic[(-1, b)]
if x in self.dic_x:
self.dic_x[x] -= 1
if self.dic_x[x] == 0:
del self.dic_x[x]
if y in self.dic_y:
self.dic_y[y] -= 1
if self.dic_y[y] == 0:
del self.dic_y[y]
def is_on(self,x,y):
return x in self.dic_x or y in self.dic_y or (1,y-x) in self.dic or (-1,y+x) in self.dic def gridIllumination(self, N, lamps, queries):
"""
:type N: int
:type lamps: List[List[int]]
:type queries: List[List[int]]
:rtype: List[int]
"""
self.dic = {}
self.dic_x = {}
self.dic_y = {}
self.dic_lamp = {}
for (x,y) in lamps:
self.turn_on(x,y)
self.dic_lamp[(x,y)] = 1
res = []
direction = [(-1,0),(1,0),(0,1),(0,-1),(1,1),(1,-1),(-1,1),(-1,-1)]
for (x,y) in queries:
if self.is_on(x,y):
res.append(1)
if (x,y) in self.dic_lamp:
self.turn_off(x, y)
for (i,j) in direction:
if (i+x,j+y) in self.dic_lamp:
self.turn_off(i+x,j+y)
else:
res.append(0)
if (x,y) in self.dic_lamp:
self.turn_off(x, y)
for (i, j) in direction:
if (i + x, j + y) in self.dic_lamp:
self.turn_off(i + x, j + y)
return res

【leetcode】1001. Grid Illumination的更多相关文章

  1. 【LeetCode】1001. Grid Illumination 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 哈希 日期 题目地址:https://leetcod ...

  2. 【LeetCode】代码模板,刷题必会

    目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...

  3. 【LeetCode】1162. 地图分析 As Far from Land as Possible(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 这个题想考察什么? 剩下的任务就是套模板! 日期 题目 ...

  4. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  5. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  6. 【LeetCode】764. Largest Plus Sign 解题报告(Python)

    [LeetCode]764. Largest Plus Sign 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...

  7. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  8. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  9. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

随机推荐

  1. springboot+jsp+mybatis项目实例(后台成功,但是无法跳转jsp页面,没有实体类的注解,看springboot+jsp第二弹相关配置,即可成功配置jsp)

    SpringBoot是用来简化SpringMvc开发的项目,这里自然要整合mybatis等持久化框架! 先看看项目目录: 一.在pom.xml中配置依赖jar包:<project xmlns=& ...

  2. Static Fields and Methods

    If you define a field as static, then there is only one such field per class. In contrast, each obje ...

  3. windows之cmd常用命令

    一.简单介绍 CMD全称command,即命令提示符,是内置在windows图形操作系统内的磁盘操作系统,通过CMD可以方便用户查询比较复杂的信息或快速查找实现某些功能等,比如说打开文件.系统设置等操 ...

  4. window和linux(centos7)安装mysql5.7

    window mysql 安装步骤 社区版本下载地址: https://dev.mysql.com/downloads/file/?id=474802 下载完成后,得到mysql-installer- ...

  5. Nmon监控服务端性能

    一.安装1.查看服务器操作系统的版本信息 lsb_release -a cat /etc/*release2.下载 a.nmon下载地址:http://nmon.sourceforge.net/pmw ...

  6. 【Dart学习】--之Duration相关方法总结

    一,概述 Duration表示从一个时间点到另一个时间点的时间差 如果是一个较晚的时间点和一个较早的时间点,Duration可能是负数 二,创建Duration 唯一的构造函数创建Duration对象 ...

  7. 全面质量管理体系运转的基本方法 PDCA

    PDCA管理循环 PDCA循环作为全面质量管理体系运转的基本方法,其实是需要搜集大量数据资料,并综合运用各种管理技术和方法.全面质量管理活动的全部过程,就是质量计划的制订和组织实现的过程,这个过程就是 ...

  8. AcWing 314. 低买 (线性DP)打卡

    题目:https://www.acwing.com/problem/content/316/ 题意:求一个最长单调递减子序列,然后并且求方案数,如果序列完全一样就不要了 思路:我们肯定时修改LIS,我 ...

  9. 专家揭秘:STM32启动过程全解

    电子发烧友网核心提示:本文主要阐述了STM32启动过程全面解析,包括启动过程的介绍.启动代码的陈列以及深入解析. 相对于ARM上一代的主流ARM7/ARM9内核架构,新一代Cortex内核架构的启动方 ...

  10. Flask学习之旅--分页功能:分别使用 flask--pagination 和分页插件 layPage

    一.前言 现在开发一个网站,分页是一个很常见的功能了,尤其是当数据达到一定量的时候,如果都显示在页面上,会造成页面过长而影响用户体验,除此之外,还可能出现加载过慢等问题.因此,分页就很有必要了. 分页 ...