【leetcode】1001. Grid Illumination
题目如下:
On a
N x Ngrid of cells, each cell(x, y)with0 <= x < Nand0 <= y < Nhas a lamp.Initially, some number of lamps are on.
lamps[i]tells us the location of thei-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 byqueries], 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 thei-th queryqueries[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 <= N <= 10^90 <= lamps.length <= 200000 <= queries.length <= 20000lamps[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的更多相关文章
- 【LeetCode】1001. Grid Illumination 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 哈希 日期 题目地址:https://leetcod ...
- 【LeetCode】代码模板,刷题必会
目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...
- 【LeetCode】1162. 地图分析 As Far from Land as Possible(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 这个题想考察什么? 剩下的任务就是套模板! 日期 题目 ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】764. Largest Plus Sign 解题报告(Python)
[LeetCode]764. Largest Plus Sign 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
随机推荐
- poj 2186: Popular Cows(tarjan基础题)
题目链接 tarjan参考博客 题意:求在图上可以被所有点到达的点的数量. 首先通过tarjan缩点,将所有内部两两可达的子图缩为一点,新图即为一个有向无环图(即DAG). 在这个DAG上,若存在不止 ...
- web项目问题总结
1.项目编码问题:建立项目之初,应统一设置项目组所有电脑的eclipse 默认项目编码,还有各个文件的编码格式 中文乱码的问题,需要三个地方同时设置utf-8编码,第一是数据库里表的编码,第二是jsp ...
- 因为信仰,油画专业的他自学开发进击阿里技术P9
大约在1年以前,阿里云视频云团队来了一位热心和气.爱好广泛的老干部新同学,他就是资深技术专家郝冲,花名和招,寓意“和气招财”. 有人说程序员只喜欢安静地写代码,和招偏偏一个户外运动爱好者.他擅长滑雪, ...
- Mac 上使用svn 记录
.启动svn服务器 svnadmin create /Users/liuwei/Desktop/svn/UI 如果本地有 UI这个目录了就不用再运行 使用这句就可以了 svnserve -d -r / ...
- php min()函数 语法
php min()函数 语法 作用:从所有参数中找到最小数 语法:min(X,Y,Z) 或者min(array(X,Y,Z)) 参数:min函数中参数至少一个,可以多个参数,也可以是数组. 说明:如果 ...
- 【CF741D】Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(dsu on tree)
题意:我们称一个字符串为周驿东串当且仅当重排它的字符可以组成一个回文串. 给出一个n个点的有根树,根为1,每条边上有一个从a到v的字符,求每个点的子树中所有简单路径可以组成的周驿东串中的最长长度. n ...
- VC++ 创建及调用Dll
一._stdcall 被这个关键字修饰的函数,其参数都是从右向左通过堆栈传递的(__fastcall 的前面部分由ecx,edx传), 函数调用在返回前要由被调用者清理堆栈. 这个关键字主要见于Mic ...
- Linux基本常用命令|ubuntu获取root权限
我用的是ubuntu12.4系统,因为默认是没有获取root的权限的 下边讲解怎么获取root权限 在终端中输入: sudo passwd root Enter new UNIX password: ...
- Houdini学习笔记——【一】散落苹果
[案例一]散落的苹果 0.渲染 1.sop使用 - 苹果主体:curve绘制刨面曲线,revolve车削得到苹果主体,uvtexture来调整uv,convert继续转换为polygon,fuse缝合 ...
- PHP连接FTP服务的简单实现
PHP连接FTP服务: <?php class Ftp { private $connect; private $getback; /** * ftp连接信息 * @var array */ p ...