Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Count Unguarded Cells in the Grid LeetCode Solution

Problem – Count Unguarded Cells in the Grid LeetCode Solution

You are given two integers m and n representing a 0-indexed m x n grid. You are also given two 2D integer arrays guards and walls where guards[i] = [rowi, coli] and walls[j] = [rowj, colj] represent the positions of the ith guard and jth wall respectively.

A guard can see every cell in the four cardinal directions (north, east, south, or west) starting from their position unless obstructed by a wall or another guard. A cell is guarded if there is at least one guard that can see it.

Return the number of unoccupied cells that are not guarded.

Example 1:

Input: m = 4, n = 6, guards = [[0,0],[1,1],[2,3]], walls = [[0,1],[2,2],[1,4]]
Output: 7
Explanation: The guarded and unguarded cells are shown in red and green respectively in the above diagram.
There are a total of 7 unguarded cells, so we return 7.

Example 2:

Input: m = 3, n = 3, guards = [[1,1]], walls = [[0,1],[1,0],[2,1],[1,2]]
Output: 4
Explanation: The unguarded cells are shown in green in the above diagram.
There are a total of 4 unguarded cells, so we return 4.

Constraints:

  • 1 <= m, n <= 105
  • 2 <= m * n <= 105
  • 1 <= guards.length, walls.length <= 5 * 104
  • 2 <= guards.length + walls.length <= m * n
  • guards[i].length == walls[j].length == 2
  • 0 <= rowi, rowj < m
  • 0 <= coli, colj < n
  • All the positions in guards and walls are unique.

Count Unguarded Cells in the Grid LeetCode Solution in C++

class Solution {
public:
    int countUnguarded(int m, int n, vector<vector<int>>& g, vector<vector<int>>& w) {
        // m is no. of rows, n is no. of columns, g is guards vector and w is walls vector
        vector<vector<int>> v(m, vector<int> (n,0));
        int k = w.size();
        for(int i=0;i<k;i++){
            int x = w[i][0], y = w[i][1];
            v[x][y] = -2;
        }
        k = g.size();
        for(int i=0;i<k;i++){
            int x = g[i][0], y = g[i][1];
            v[x][y] = 2;
        }
        for(int j=0;j<k;j++){
            int x = g[j][0], y = g[j][1];
            for(int i=x-1;i>=0;i--){ // up
                if(v[i][y]==-2 || v[i][y]==2) break;
                v[i][y] = 1;
            }
            for(int i=x+1;i<m;i++){ // down
                if(v[i][y]==-2 || v[i][y]==2) break;
                v[i][y] = 1;
            }
            for(int i=y-1;i>=0;i--){ // left
                if(v[x][i]==-2 || v[x][i]==2) break;
                v[x][i] = 1;
            }
            for(int i=y+1;i<n;i++){ // right
                if(v[x][i]==-2 || v[x][i]==2) break;
                v[x][i] = 1;
            }
        }
        int ans = 0;
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(!v[i][j]) ans++;
            }
        }
        return ans;
    }
};

Count Unguarded Cells in the Grid LeetCode Solution in Python

class Solution:
    def countUnguarded(self, m: int, n: int, guards: List[List[int]], walls: List[List[int]]) -> int:
        dp = [[0] * n for _ in range(m)]
        for x, y in guards+walls:
            dp[x][y] = 1
               
        directions = [(0, 1), (1, 0), (-1, 0), (0, -1)]
        
        for x, y in guards:
            for dx, dy in directions:
                curr_x = x
                curr_y = y
                
                while 0 <= curr_x+dx < m and 0 <= curr_y+dy < n and dp[curr_x+dx][curr_y+dy] != 1:
                    curr_x += dx
                    curr_y += dy
                    dp[curr_x][curr_y] = 2
                    
        return sum(1 for i in range(m) for j in range(n) if dp[i][j] == 0)                    

Count Unguarded Cells in the Grid LeetCode Solution in Java

class Solution {
    public int countUnguarded(int m, int n, int[][] guards, int[][] walls) {
        int[][] res = new int[m][n];   //We create the matrix with the proper dimension
        int result = 0, cnti, cntj;
        for(int[] i : walls){       //We insert all the walls
            res[i[0]][i[1]] = 2;
        }
        for(int[] i : guards){      //We insert all the guards
            res[i[0]][i[1]] = 1;  
        }
        for(int i = 0; i < res.length;i++){
            for(int j = 0; j < res[i].length;j++){
                if(res[i][j] == 1){     //If we found a guard...
                    cnti = i;   //Position of the guard
                    cntj = j;   //Position of the guard
                    while(cnti-1 != -1 && res[cnti-1][cntj] != 2 && res[cnti-1][cntj] != 1){  //If we can go up in the matrix...
                        res[cnti-1][cntj] = 3;
                        cnti--;
                    }
                    cnti = i;  //We reset the value to the initial one
                    while(cnti+1 != m && res[cnti+1][cntj] != 2 && res[cnti+1][cntj] != 1){   //If we can go down in the matrix...
                        res[cnti+1][cntj] = 3;
                        cnti++;
                    }
                    cnti = i;  //We reset the value to the initial one
                    while(cntj-1 != -1 && res[cnti][cntj-1] != 2 && res[cnti][cntj-1] != 1){  //If we can go to left in the matrix...
                        res[cnti][cntj-1] = 3;
                        cntj--;
                    }
                    cntj = j;  //We reset the value to the initial one
                    while(cntj+1 != n && res[cnti][cntj+1] != 2 && res[cnti][cntj+1] != 1){  //If we can go to rigth in the matrix...
                        res[cnti][cntj+1] = 3;
                        cntj++;
                    }
                }
            }
        }
        for(int[] i : res){   //Once we have marked the correct squares in the matrix...
            for(int j : i){
                if(j == 0){   //If we find a '0', we add one to the counter as it is an unguarded cell
                    result++;
                }
            }
        }
        return result;  //We return the number of unguarded cells
    }
}
Count Unguarded Cells in the Grid LeetCode Solution Review:

In our experience, we suggest you solve this Count Unguarded Cells in the Grid LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Count Unguarded Cells in the Grid LeetCode Solution

Find on LeetCode

Conclusion:

I hope this Count Unguarded Cells in the Grid LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published. Required fields are marked *