Advertisement

Detect Cycles in 2D Grid - LeetCode 1559 Solution

Detect Cycles in 2D Grid - Complete Solution Guide

Detect Cycles in 2D Grid is LeetCode problem 1559, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given a 2D array of characters grid of size m x n , you need to find if there exists any cycle consisting of the same value in grid . A cycle is a path of length 4 or more in the grid that starts and ends at the same cell. From a given cell, you can move to one of the cells adjacent to it - in one of the four directions (up, down, left, or right), if it has the same value of the current cell. Also, you cannot move to the cell that you visited in your last move. For example, the cycle (1, 1) -> (

Detailed Explanation

The problem asks us to determine if there exists a cycle in a 2D grid of characters, where a cycle is defined as a path of length 4 or more that starts and ends at the same cell, and all cells in the cycle must have the same value. Movement between cells is restricted to the four cardinal directions (up, down, left, right), and you cannot immediately return to the cell you just came from. The grid contains only lowercase English letters.

Solution Approach

The provided solution uses the Union-Find algorithm to detect cycles in the grid. Each cell in the grid is treated as a node in a graph, and edges are formed between adjacent cells with the same value. The Union-Find data structure is used to maintain the connected components of this graph. The algorithm iterates through each cell in the grid, and for each cell, it checks its right and down neighbors. If a neighbor has the same value as the current cell, the algorithm attempts to union the corresponding sets of the two cells. If the two cells already belong to the same set, then adding an edge between them would create a cycle, and the algorithm returns true. If the algorithm iterates through the entire grid without finding a cycle, it returns false.

Step-by-Step Algorithm

  1. Step 1: Initialize a Union-Find data structure with `m * n` nodes, where `m` is the number of rows and `n` is the number of columns in the grid. Each cell in the grid is mapped to a unique integer in the range [0, m*n-1]. Initially, each cell is in its own set.
  2. Step 2: Iterate through each cell (r, c) in the grid.
  3. Step 3: For each cell (r, c), check its right neighbor (r, c+1) and its down neighbor (r+1, c).
  4. Step 4: If a neighbor exists and has the same value as the current cell, attempt to 'union' the current cell and the neighbor using the Union-Find data structure.
  5. Step 5: The 'union' operation first finds the root of the sets containing both cells. If the roots are the same, then the cells are already in the same set, and adding an edge between them would create a cycle. In this case, return `true`.
  6. Step 6: If the roots are different, then merge the two sets by setting the parent of one root to the other root. This indicates that these two cells are now part of the same connected component.
  7. Step 7: If after iterating through all the cells, no cycle is detected, return `false`.

Key Insights

  • Insight 1: The problem can be effectively solved using the Union-Find (Disjoint Set Union) data structure. This allows us to efficiently track connected components of cells with the same value.
  • Insight 2: The essence of detecting a cycle lies in identifying if two adjacent cells with the same value already belong to the same connected component. If they do, merging them would create a cycle.
  • Insight 3: The grid indices need to be converted into a 1D representation for the Union-Find data structure, as Union-Find operates on a set of integers.

Complexity Analysis

Time Complexity: O(m*n)

Space Complexity: O(m*n)

Topics

This problem involves: Array, Depth-First Search, Breadth-First Search, Union Find, Matrix.

Companies

Asked at: Nutanix.