LeetCode 2617: Minimum Number of Visited Cells in a Grid Solution

Master LeetCode problem 2617 (Minimum Number of Visited Cells in a Grid), a hard 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.

Problem Explanation

Explanation

To solve this problem, we can use a breadth-first search (BFS) approach. We start from the top-left cell and explore all possible cells that can be reached from the current cell. We keep track of the minimum number of cells visited to reach each cell. If we reach the bottom-right cell, we return the minimum number of cells visited. If we can't reach the bottom-right cell, we return -1.

  1. Start from the top-left cell (0, 0) and add it to the queue.
  2. While the queue is not empty, pop a cell from the queue.
  3. Explore all possible cells that can be reached from this cell based on the constraints given in the problem.
  4. If a cell can be reached with a smaller number of visited cells, update the number of visited cells for that cell and add it to the queue.
  5. Continue until we reach the bottom-right cell or exhaust all possible paths.

Time Complexity: O(m * n) - as we visit each cell only once. Space Complexity: O(m * n) - for the queue and visited array.

Solution Code

class Solution {
    public int minVisitedCells(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        
        Queue<int[]> queue = new LinkedList<>();
        queue.offer(new int[]{0, 0, 1}); // {row, col, visited cells}
        
        boolean[][] visited = new boolean[m][n];
        visited[0][0] = true;
        
        int[][] directions = {{0, 1}, {1, 0}};
        
        while (!queue.isEmpty()) {
            int[] curr = queue.poll();
            int row = curr[0];
            int col = curr[1];
            int visitedCells = curr[2];
            
            if (row == m - 1 && col == n - 1) {
                return visitedCells;
            }
            
            for (int[] dir : directions) {
                int newRow = row + dir[0];
                int newCol = col + dir[1];
                
                if (newRow < m && newCol < n && !visited[newRow][newCol] && visitedCells + 1 <= grid[row][col] + ((dir[0] == 0) ? col : row)) {
                    visited[newRow][newCol] = true;
                    queue.offer(new int[]{newRow, newCol, visitedCells + 1});
                }
            }
        }
        
        return -1;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2617 (Minimum Number of Visited Cells in a Grid)?

This page provides optimized solutions for LeetCode problem 2617 (Minimum Number of Visited Cells in a Grid) 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 2617 (Minimum Number of Visited Cells in a Grid)?

The time complexity for LeetCode 2617 (Minimum Number of Visited Cells in a Grid) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2617 on DevExCode?

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

Back to LeetCode Solutions