Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Count Unguarded Cells in the Grid

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "2257" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

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.

Explanation

Here's the breakdown of the solution:

  • Represent the grid: Use a 2D array to represent the grid. Initially, mark all cells as unoccupied. Then, mark the positions of guards and walls accordingly.
  • Guard Visibility: Iterate through each guard and simulate their visibility in all four directions (north, east, south, west). Mark all the cells visible to the guard as guarded. Stop the visibility when a wall or another guard is encountered.
  • Count Unguarded Cells: Finally, iterate through the grid and count the number of unoccupied cells that are not guarded.

  • Time and Space Complexity: O(m*n) time complexity, O(m*n) space complexity

Code

    def countUnguarded(m: int, n: int, guards: list[list[int]], walls: list[list[int]]) -> int:
    """
    Counts the number of unguarded cells in a grid.

    Args:
        m: The number of rows in the grid.
        n: The number of columns in the grid.
        guards: A list of guard positions.
        walls: A list of wall positions.

    Returns:
        The number of unguarded cells.
    """

    grid = [[0] * n for _ in range(m)]  # 0: unguarded, 1: wall, 2: guard, 3: guarded

    # Mark walls
    for row, col in walls:
        grid[row][col] = 1

    # Mark guards
    for row, col in guards:
        grid[row][col] = 2

    # Simulate guard visibility
    for row, col in guards:
        # North
        for i in range(row - 1, -1, -1):
            if grid[i][col] == 1 or grid[i][col] == 2:
                break
            grid[i][col] = 3

        # East
        for j in range(col + 1, n):
            if grid[row][j] == 1 or grid[row][j] == 2:
                break
            grid[row][j] = 3

        # South
        for i in range(row + 1, m):
            if grid[i][col] == 1 or grid[i][col] == 2:
                break
            grid[i][col] = 3

        # West
        for j in range(col - 1, -1, -1):
            if grid[row][j] == 1 or grid[row][j] == 2:
                break
            grid[row][j] = 3

    # Count unguarded cells
    unguarded_count = 0
    for i in range(m):
        for j in range(n):
            if grid[i][j] == 0:
                unguarded_count += 1

    return unguarded_count

More from this blog

C

Chatmagic blog

2894 posts