Similar Problems

Similar Problems not available

Erect The Fence - Leetcode Solution


  • amazon

LeetCode:  Erect The Fence Leetcode Solution

Difficulty: Hard

Topics: math array  

Problem Description:

There is a rectangular grid of integers in which some cells are blocked. You are given a list of integers heights where heights[i] represents the height of and opened cell at index i. You are also given integers row and gate where (row, gate) represents that the cell (row, gate) is blocked. Each cell is adjacent to its four adjacent cells(i.e., up, down, left, and right).

You want to build a fence such that it surrounds the rectangular grid and has the following properties:

  • The fence should be continuous and have all its corners in the grid.
  • The fence should have a minimum height. That is, the height of the fence should be the least among all fences that can be built to surround the grid.
  • The fence should not intersect any cell that is not blocked.

Return an integer array answer where answer = [isPossible, height], where isPossible is True if it is possible to build such a fence, and height is the minimum height of the fence. If it is not possible to build a fence according to the above rules, return an empty array.

Example 1:

Input: heights = [1,2,2,1], rows = 1, gate = 3 Output: [true, 1] Explanation: The fence can be built around the shaded area with a minimum height of 1.

Example 2:

Input: heights = [1,2,3,4], rows = 2, gate = 2 Output: [false] Explanation: It is impossible to build a fence around the given grid since it would intersect the first cell.


To solve this problem, we need to find a path that surrounds the blocked cell, and at the same time, this path should have the minimum height. To achieve this, we will use BFS to find the shortest path that encloses the blocked cell.

We will start BFS from all four sides(i.e., top, bottom, left, and right) of the blocked cell one by one and save the path and its minimum height that we find. After finding all four paths, we will choose the one with the minimum height.


  1. First, find the index of the blocked cell using (row, gate)
  2. Initialize an empty queue, visited set, and max-height = 0
  3. For each of the four sides(i.e., top, bottom, left, and right) of the blocked cell:
    • Do BFS starting from the index of the blocked cell in that particular direction
    • While doing BFS, maintain a path and the minimum height of the cells in that path
    • If the path encloses the rectangular grid and it has a minimum height less than max-height:
      • set max-height to the minimum height of this path
      • add the path to a list of valid paths
  4. If no valid paths were found, return empty array
  5. If one or more valid paths were found, choose the one with the minimum height and return [true, min_height]

Complexity Analysis:

  1. Time Complexity: O(NM), where N is the number of rows and M is the number of columns of the rectangular grid.
  2. Space Complexity: O(NM), to store the visited set.

Erect The Fence Solution Code