2

I am working on Leetcode 417 Pacific Atlantic Water Flow:

There is an m x n rectangular island that borders both the Pacific Ocean and Atlantic Ocean. The Pacific Ocean touches the island's left and top edges, and the Atlantic Ocean touches the island's right and bottom edges.

The island is partitioned into a grid of square cells. You are given an m x n integer matrix heights where heights[r][c] represents the height above sea level of the cell at coordinate (r, c).

The island receives a lot of rain, and the rain water can flow to neighboring cells directly north, south, east, and west if the neighboring cell's height is less than or equal to the current cell's height. Water can flow from any cell adjacent to an ocean into the ocean.

Return a 2D list of grid coordinates result where result[i] = [ri, ci] denotes that rain water can flow from cell (ri, ci) to both the Pacific and Atlantic oceans.

My solution is below. I am having Time Limit Exceeded error for a very large test case like

[[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19],[72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,20],[71,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,90,21],[70,135,192,193,194,195,196,197,198,199,200,201,202,203,204,205,152,91,22], ...]

I cannot figure out why my BFS did not work within a reasonable time. What am I missing?

class Solution:
    def pacificAtlantic(self, heights):
        rows, cols = len(heights), len(heights[0])

        # define directions
        directions = [[0, 1], [0, -1], [1, 0], [-1, 0]]
        def bfs(node, visited):
            Q = [node]
            while Q:
                x, y = Q.pop(0)
                visited[x][y] = True
                for dx, dy in directions:
                    next_x, next_y = x + dx, y + dy
                    if next_x < 0 or next_x >= rows:
                        continue
                    if next_y < 0 or next_y >= cols:
                        continue
                    if visited[next_x][next_y]:
                        continue
                    if heights[x][y] > heights[next_x][next_y]:
                        continue

                    Q.append((next_x, next_y))
        
        # pacific
        pacific_start = [[0, i] for i in range(cols)] + [[i, 0] for i in range(1, rows)]
        pacific_visited = [[False for _ in range(cols)] for _ in range(rows)]
        for row, col in pacific_start:
            if not pacific_visited[row][col]:
                bfs((row, col), pacific_visited, 0)

        # atlantic
        atlantic_start = [[rows - 1, i] for i in range(cols)] + [[i, cols - 1] for i in range(0, rows - 1)]
        atlantic_visited = [[False for _ in range(cols)] for _ in range(rows)]
        for row, col in atlantic_start:
            if not atlantic_visited[row][col]:
                bfs((row, col), atlantic_visited, 0)
  
        # find the common land
        ans = []
        for i in range(rows):
            for j in range(cols):
                if pacific_visited[i][j] and atlantic_visited[i][j]:
                    ans.append((i, j))
        return ans
0

2 Answers 2

2

There are two issues in your bfs function that negatively affect performance:

  1. pop(0) is not efficient on a list. Instead use a deque
  2. You mark a node as visited, after having taken it from the queue, but that means you can have several copies of the same cell in the queue, increasing the number of iterations the BFS loop will make. Instead mark a node as visited at the time you push it on the queue.

Here is the code of your bfs function, with minimal changes needed to resolve those two issues:

def bfs(node, visited, test):
    visited[node[0]][node[1]] = True # mark node when it enters the queue
    Q = deque([node])  # Use a deque, not a list
    while Q:
        x, y = Q.popleft()  # now it's an efficient operation
        for dx, dy in directions:
            next_x, next_y = x + dx, y + dy
            if next_x < 0 or next_x >= rows:
                continue
            if next_y < 0 or next_y >= cols:
                continue
            if visited[next_x][next_y]:
                continue
            if heights[x][y] > heights[next_x][next_y]:
                continue
            visited[next_x][next_y] = True # mark node when it enters the queue
            Q.append((next_x, next_y))
Sign up to request clarification or add additional context in comments.

3 Comments

pop(0) is not efficient on a list really?
@JohnGordon, not as efficient as deque's popleft. Quoted from the docs: "Though list objects support similar operations, they are optimized for fast fixed-length operations and incur O(𝑛) memory movement costs for pop(0) and insert(0, v) operations which change both the size and position of the underlying data representation."
@JohnGordon Would you expect it to be constant time?
1

You can also use set() and simplify your conditions:

class Solution:
    def pacificAtlantic(self, heights):
        if not heights:
            return []

        rows, cols = len(heights), len(heights[0])
        pacific = set([(row, 0) for row in range(rows)] + [(0, col) for col in range(1, cols)])
        atlantic = set([(row, cols - 1) for row in range(rows)] + [(rows - 1, col) for col in range(cols - 1)])
        directions = ((0, 1), (0, -1), (1, 0), (-1, 0))
        def bfs(ocean):
            Q = collections.deque(ocean)
            while Q:
                x, y = Q.popleft()
                for dx, dy in directions:
                    if 0 <= dx + x < rows and 0 <= dy + y < cols and (dx + x, dy + y) not in ocean and heights[dx + x][dy + y] >= heights[x][y]:
                        Q.append((dx + x, dy + y))
                        ocean.add((dx + x, dy + y))

            return ocean


        return tuple(bfs(pacific) & bfs(atlantic))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.