zoukankan      html  css  js  c++  java
  • 【leetcode】1001. Grid Illumination

    题目如下:

    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
  • 相关阅读:
    ffmpeg结构体以及函数介绍(三)
    FFMPEG解码流程(转)
    ffmpeg结构体以及函数介绍(一)
    摄像头视频采集压缩及传输
    bedework文档(开始简单部分)
    【实时数据库PISDK】关于PITime的悲剧
    【项目研究】自动安装并配置ODBC的思路与实现
    【读书笔记】动态链接库
    如何最快测试CPU是大端机还是小端机?
    学习编程的五条捷径
  • 原文地址:https://www.cnblogs.com/seyjs/p/10474841.html
Copyright © 2011-2022 走看看