{"id":22513442,"url":"https://github.com/emahtab/shortest-bridge","last_synced_at":"2026-03-19T23:02:40.163Z","repository":{"id":261356085,"uuid":"875092914","full_name":"eMahtab/shortest-bridge","owner":"eMahtab","description":"Find minimum number of flips required to make connect two disconnected islands","archived":false,"fork":false,"pushed_at":"2024-11-06T04:36:31.000Z","size":10,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-02T03:19:13.703Z","etag":null,"topics":["bfs","dfs","leetcode"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eMahtab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-19T04:47:22.000Z","updated_at":"2024-11-06T04:36:34.000Z","dependencies_parsed_at":"2024-11-06T05:39:12.293Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/shortest-bridge","commit_stats":null,"previous_names":["emahtab/shortest-bridge"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fshortest-bridge","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fshortest-bridge/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fshortest-bridge/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fshortest-bridge/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/shortest-bridge/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245950637,"owners_count":20699102,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bfs","dfs","leetcode"],"created_at":"2024-12-07T03:12:21.857Z","updated_at":"2026-01-07T12:04:39.285Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Shortest Bridge\n## https://leetcode.com/problems/shortest-bridge\nYou are given an n x n binary matrix grid where 1 represents land and 0 represents water.\n\nAn island is a 4-directionally connected group of 1's not connected to any other 1's. There are exactly two islands in grid.\n\nYou may change 0's to 1's to connect the two islands to form one island.\n\nReturn the smallest number of 0's you must flip to connect the two islands.\n\n```\nExample 1:\n\nInput: grid = [[0,1],[1,0]]\nOutput: 1\n\nExample 2:\n\nInput: grid = [[0,1,0],[0,0,0],[0,0,1]]\nOutput: 2\n\nExample 3:\n\nInput: 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]]\nOutput: 1\n``` \n\n## Constraints:\n\n1. n == grid.length == grid[i].length\n2. 2 \u003c= n \u003c= 100\n3. grid[i][j] is either 0 or 1.\n4. There are exactly two islands in grid.\n\n# Implementation 1 : Using BFS try to reach any `1` of the other island (DFS+BFS)\n```java\nclass Solution {\n    public int shortestBridge(int[][] grid) {\n        int rows = grid.length;\n        int cols = grid[0].length;\n        Queue\u003cint[]\u003e queue = new LinkedList\u003c\u003e();\n        boolean firstIslandFound = false;\n        for(int row = 0; row \u003c rows \u0026\u0026 !firstIslandFound; row++) {\n            for(int col = 0; col \u003c cols; col++) {\n                if(grid[row][col] == 1) {\n                    firstIslandFound = true;\n                    dfs(grid, row, col, queue);\n                    break;\n                }\n            }\n        }\n        int distance = 0;\n        int[][] directions = {{0, 1}, {0,-1}, {-1, 0}, {1, 0}};\n        Set\u003cString\u003e visited = new HashSet\u003c\u003e();\n        while(!queue.isEmpty()) {\n            int size = queue.size();\n            for(int i = 1; i \u003c= size; i++) {\n                int[] pos = queue.remove();\n                String location = pos[0]+\",\"+pos[1];\n                if(visited.contains(location)) continue;\n                visited.add(location);\n                for(int[] direction : directions) {\n                    int x = pos[0] + direction[0];\n                    int y = pos[1] + direction[1];\n                    if(x \u003c 0 || x \u003e= grid.length || y \u003c 0 || y \u003e= grid[0].length)\n                      continue;\n                    if(grid[x][y] == 1)\n                      return distance;  \n                    else if(grid[x][y] == 0) {\n                        String key = x+\",\"+y;\n                        if(!visited.contains(key))\n                          queue.add(new int[]{x,y});\n                    }    \n                }\n            }\n            distance++;\n        }\n        return -1;\n    }\n\n    private void dfs(int[][] grid, int row, int col, Queue\u003cint[]\u003e queue) {\n        if(row \u003c 0 || row \u003e= grid.length || col \u003c 0 || col \u003e= grid[0].length || grid[row][col] != 1)\n          return;\n\n        grid[row][col] = 2;\n        queue.add(new int[]{row, col});\n        dfs(grid, row, col + 1, queue);\n        dfs(grid, row, col - 1, queue);\n        dfs(grid, row - 1, col, queue);\n        dfs(grid, row + 1, col, queue);  \n    }\n}\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fshortest-bridge","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fshortest-bridge","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fshortest-bridge/lists"}