LeetCode 934: Shortest Bridge Solution

Master LeetCode problem 934 (Shortest Bridge), 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.

Problem Explanation

Explanation

To solve this problem, we can follow these steps:

  1. Use depth-first search (DFS) to label the two islands separately with different numbers.
  2. Use breadth-first search (BFS) to expand from one island towards the other, keeping track of the number of flips needed to connect the two islands.
  3. Stop the BFS when we reach the other island, and return the number of flips needed.

Time complexity: O(n^2)
Space complexity: O(n^2)

Solution Code

class Solution {
    public int shortestBridge(int[][] grid) {
        int n = grid.length;
        boolean[][] visited = new boolean[n][n];
        Queue<int[]> queue = new LinkedList<>();
        boolean found = false;
        
        // DFS to label the two islands with different numbers
        for (int i = 0; i < n; i++) {
            if (found) break;
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 1) {
                    dfs(grid, i, j, visited, queue);
                    found = true;
                    break;
                }
            }
        }
        
        int steps = 0;
        int[][] directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
        
        // BFS to expand from one island towards the other
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                int[] curr = queue.poll();
                for (int[] dir : directions) {
                    int x = curr[0] + dir[0];
                    int y = curr[1] + dir[1];
                    if (x >= 0 && x < n && y >= 0 && y < n && !visited[x][y]) {
                        if (grid[x][y] == 1) {
                            return steps;
                        }
                        visited[x][y] = true;
                        queue.offer(new int[]{x, y});
                    }
                }
            }
            steps++;
        }
        
        return -1;
    }
    
    private void dfs(int[][] grid, int i, int j, boolean[][] visited, Queue<int[]> queue) {
        if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || visited[i][j] || grid[i][j] == 0) {
            return;
        }
        
        visited[i][j] = true;
        queue.offer(new int[]{i, j});
        
        dfs(grid, i + 1, j, visited, queue);
        dfs(grid, i - 1, j, visited, queue);
        dfs(grid, i, j + 1, visited, queue);
        dfs(grid, i, j - 1, visited, queue);
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 934 (Shortest Bridge)?

This page provides optimized solutions for LeetCode problem 934 (Shortest Bridge) 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 934 (Shortest Bridge)?

The time complexity for LeetCode 934 (Shortest Bridge) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 934 on DevExCode?

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

Back to LeetCode Solutions