leeetcode 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

解題思路:

設置一個set<pair<int , int>> lampset,  存放每個燈的{x , y} ;

設置unordered_map<int , int> xbright , ybright , posi_diag , nega_diag ;

xbright[x]=cnt : 有cnt盞燈使橫座標爲x的那一列亮

ybright[y] = cnt : 有cnt盞燈使縱座標爲y的那一行亮

posi_diag[x - y] = cnt ; 有cnt盞燈使{x , y}的正對角線亮 ;

nega_diag[x + y] = cnt : 有cnt盞燈使{x , y}的反對角線亮 ;

對於queries中的每一個{x , y} ,找其同一行同一列,正對角線,反對角線是否是亮的,只要一條線亮,那這個位置就是亮的 ;

if(xbright.count(x) || ybright.count(y) || posi_diag.count(x - y) || nega_diag.count(x + y)) res[i] = 1 ;

然後查找包括自身和周圍8個方向上是否有燈,如果有燈,則從lampset中刪掉燈 , 並將相應行,列和對角線上的燈數-1 ;

class Solution {
public:
    vector<int> gridIllumination(int N, vector<vector<int>>& lamps, vector<vector<int>>& queries) 
    {
        set<pair<int , int>> lampset ;
        unordered_map<int , int> xbright , ybright , posi_diag , nega_diag ;
        vector<int> res(queries.size() , 0) ;
        
        for(int i = 0 ; i < lamps.size() ; ++i)
        {
            lampset.insert({lamps[i][0] , lamps[i][1]}) ;
            xbright[lamps[i][0]]++ ;
            ybright[lamps[i][1]]++ ;
            posi_diag[lamps[i][0] - lamps[i][1]]++ ;
            nega_diag[lamps[i][0] + lamps[i][1]]++ ;
        }   

        for(int i = 0 ; i < queries.size() ; i++)
        {
            int x = queries[i][0] , y = queries[i][1] ;
            if(xbright.count(x) || ybright.count(y) || posi_diag.count(x - y) || nega_diag.count(x + y)) res[i] = 1 ;
            
            for(int j = 0 ; j < 9 ; ++j)
            {
                int new_x = x + dir[j][0] , new_y = y + dir[j][1] ;
                if(lampset.count({new_x , new_y}))
                {
                    lampset.erase({new_x , new_y}) ;
                    
                    xbright[new_x]-- ;
                    if(xbright[new_x] == 0) xbright.erase(new_x) ;
                    
                    ybright[new_y]-- ;
                    if(ybright[new_y] == 0) ybright.erase(new_y) ;
                    
                    posi_diag[new_x - new_y]-- ;
                    if(posi_diag[new_x - new_y] == 0) posi_diag.erase(new_x - new_y) ;
                    
                    nega_diag[new_x + new_y]-- ;
                    if(nega_diag[new_x + new_y] == 0) nega_diag.erase(new_x + new_y) ;
                }
            }
        }
        
        return res ;
    }
    
private:
    
    vector<vector<int>> dir = {{0 , 0} ,{0 , 1} , {1 , 1} , {1 , 0} , {1 , -1} , {0 , -1} , {-1 , -1} , {-1 , 0} , {-1 , 1}} ;
};

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章