934. Shortest Bridge Medium

@problem@discussion
#Array#Depth-First Search#Breadth-First Search#Matrix



1/**
2 * [934] Shortest Bridge
3 *
4 * You are given an n x n binary matrix grid where 1 represents land and 0 represents water.
5 * An island is a 4-directionally connected group of 1's not connected to any other 1's. There are exactly two islands in grid.
6 * You may change 0's to 1's to connect the two islands to form one island.
7 * Return the smallest number of 0's you must flip to connect the two islands.
8 *  
9 * Example 1:
10 * 
11 * Input: grid = [[0,1],[1,0]]
12 * Output: 1
13 * 
14 * Example 2:
15 * 
16 * Input: grid = [[0,1,0],[0,0,0],[0,0,1]]
17 * Output: 2
18 * 
19 * Example 3:
20 * 
21 * Input: grid = [[1,1,1,1,1],[1,0,0,0,1],[1,0,1,0,1],[1,0,0,0,1],[1,1,1,1,1]]
22 * Output: 1
23 * 
24 *  
25 * Constraints:
26 * 
27 * 	n == grid.length == grid[i].length
28 * 	2 <= n <= 100
29 * 	grid[i][j] is either 0 or 1.
30 * 	There are exactly two islands in grid.
31 * 
32 */
33pub struct Solution {}
34
35// problem: https://leetcode.com/problems/shortest-bridge/
36// discuss: https://leetcode.com/problems/shortest-bridge/discuss/?currentPage=1&orderBy=most_votes&query=
37
38// submission codes start here
39
40impl Solution {
41    pub fn shortest_bridge(grid: Vec<Vec<i32>>) -> i32 {
42        0
43    }
44}
45
46// submission codes end
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51
52    #[test]
53    fn test_934() {
54    }
55}
56


Back
© 2025 bowen.ge All Rights Reserved.