LeetCode 2077: Paths in Maze That Lead to Same Room Solution

Master LeetCode problem 2077 (Paths in Maze That Lead to Same Room), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2077. Paths in Maze That Lead to Same Room

Medium

Problem Explanation

Explanation

To solve this problem, we can use depth-first search (DFS) starting from each room in the maze to explore all possible paths. During the traversal, we keep track of the current path taken to reach each room. If we encounter a room that has been visited before with the same path, we can conclude that there is a cycle in the maze leading back to that room. We can then store the path as a result.

Time Complexity

The time complexity of this approach is O(N^2), where N is the number of rooms in the maze, as we might visit each room multiple times during the DFS traversal.

Space Complexity

The space complexity is O(N) to store the visited rooms and paths.

Solution Code

import java.util.*;

class Solution {
    public List<List<Integer>> pathsWithSameRoom(List<List<Integer>> maze) {
        List<List<Integer>> result = new ArrayList<>();
        Map<Integer, List<List<Integer>>> pathMap = new HashMap<>();
        boolean[][] visited = new boolean[maze.size()][maze.get(0).size()];

        for (int i = 0; i < maze.size(); i++) {
            for (int j = 0; j < maze.get(0).size(); j++) {
                if (!visited[i][j]) {
                    List<Integer> path = new ArrayList<>();
                    if (dfs(maze, visited, i, j, i, j, path)) {
                        int hash = Objects.hash(path);
                        if (!pathMap.containsKey(hash)) {
                            pathMap.put(hash, new ArrayList<>());
                        }
                        pathMap.get(hash).add(path);
                    }
                }
            }
        }

        for (List<List<Integer>> paths : pathMap.values()) {
            if (paths.size() > 1) {
                result.addAll(paths);
            }
        }

        return result;
    }

    private boolean dfs(List<List<Integer>> maze, boolean[][] visited, int i, int j, int startI, int startJ, List<Integer> path) {
        if (visited[i][j]) {
            return false;
        }

        visited[i][j] = true;
        path.add(i);
        path.add(j);

        int[] dirs = {0, 1, 0, -1, 0};
        for (int k = 0; k < 4; k++) {
            int ni = i + dirs[k];
            int nj = j + dirs[k + 1];
            if (ni >= 0 && ni < maze.size() && nj >= 0 && nj < maze.get(0).size() && maze.get(i).get(j).equals(maze.get(ni).get(nj))) {
                if ((ni != startI || nj != startJ) && dfs(maze, visited, ni, nj, startI, startJ, path)) {
                    return true;
                }
            }
        }

        return true;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2077 (Paths in Maze That Lead to Same Room)?

This page provides optimized solutions for LeetCode problem 2077 (Paths in Maze That Lead to Same Room) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2077 (Paths in Maze That Lead to Same Room)?

The time complexity for LeetCode 2077 (Paths in Maze That Lead to Same Room) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2077 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2077 in Java, C++, or Python.

Back to LeetCode Solutions