{"id":22514012,"url":"https://github.com/emahtab/01-matrix","last_synced_at":"2026-02-03T10:32:29.182Z","repository":{"id":79525214,"uuid":"242276380","full_name":"eMahtab/01-matrix","owner":"eMahtab","description":"01 Matrix","archived":false,"fork":false,"pushed_at":"2021-02-16T02:05:01.000Z","size":18,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-24T09:49:02.544Z","etag":null,"topics":["leetcode","matrix","problem-solving"],"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":"2020-02-22T04:00:46.000Z","updated_at":"2021-02-16T02:05:03.000Z","dependencies_parsed_at":"2023-05-10T17:00:43.475Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/01-matrix","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/eMahtab/01-matrix","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2F01-matrix","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2F01-matrix/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2F01-matrix/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2F01-matrix/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/01-matrix/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2F01-matrix/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29041363,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-03T10:09:22.136Z","status":"ssl_error","status_checked_at":"2026-02-03T10:09:16.814Z","response_time":96,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["leetcode","matrix","problem-solving"],"created_at":"2024-12-07T03:15:35.960Z","updated_at":"2026-02-03T10:32:29.167Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# 01-matrix\nGiven a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.\n\nThe distance between two adjacent cells is 1.\n```\nExample 1:\n\nInput:\n[[0,0,0],\n [0,1,0],\n [0,0,0]]\n\nOutput:\n[[0,0,0],\n [0,1,0],\n [0,0,0]]\n \nExample 2:\n\nInput:\n[[0,0,0],\n [0,1,0],\n [1,1,1]]\n\nOutput:\n[[0,0,0],\n [0,1,0],\n [1,2,1]]\n \n```\n**Note:**\n\n1. The number of elements of the given matrix will not exceed 10,000.\n2. There are at least one 0 in the given matrix.\n3. The cells are adjacent in only four directions: up, down, left and right.\n\n## Approach :\nWe can't solve this problem in one pass through the matrix, as it will lead to wrong result.\n```\ne.g. \n{0, 1, 1, 1},\n{1, 1, 1, 1},\n{1, 1, 1, 1},\n{1, 1, 1, 0}\n```\nIf we just see the four adjacent cells of a cell to determine the result in one pass, it will lead to wrong result for the above matrix. Correct answer will be :\n```\n{0, 1, 2, 3},\n{1, 2, 3, 2},\n{2, 3, 2, 1},\n{3, 2, 1, 0}\n```\n\n## Implementation 1 : BFS Approach\n```java\nclass Solution {\n    private class Position {\n        int row, column;\n        Position(int row, int column) {\n            this.row = row;\n            this.column = column;\n        }\n    }\n    public int[][] updateMatrix(int[][] matrix) {\n        if(matrix == null || matrix.length == 0)\n            return matrix;\n        \n        int rows = matrix.length;\n        int cols = matrix[0].length;\n        \n        Queue\u003cPosition\u003e q = new LinkedList\u003c\u003e();\n        for(int i = 0; i \u003c rows; i++) {\n            for(int j = 0; j \u003c cols; j++) {\n                if(matrix[i][j] == 0)\n                    q.add(new Position(i, j));\n                else\n                    matrix[i][j] = -1;\n            }\n        }\n        int[][] directions = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};\n        int distance = 0;\n        while(!q.isEmpty()) {\n            distance++;\n            int size = q.size();\n            for(int i = 0; i \u003c size; i++) {\n                Position pos = q.remove();\n                for(int[] direction : directions) {\n                    int r = pos.row + direction[0];\n                    int c = pos.column + direction[1];\n                    if(r \u003e= 0 \u0026\u0026 r \u003c matrix.length \u0026\u0026 c \u003e= 0 \u0026\u0026 c \u003c matrix[0].length \u0026\u0026 \n                        matrix[r][c] == -1) {\n                        matrix[r][c] = distance;\n                        q.add(new Position(r, c));\n                    }\n                }\n            }\n        }\n       return matrix; \n    }\n}\n```\n\n## Implementation 1 : Alternate approach\n```java\nclass Solution {\n    public int[][] updateMatrix(int[][] matrix) {\n        if(matrix == null || matrix.length == 0)\n            return matrix;\n        \n        Queue\u003cint[]\u003e q = new LinkedList\u003c\u003e();\n        int rows = matrix.length, cols = matrix[0].length;\n        for(int i = 0; i \u003c rows; i++) {\n            for(int j = 0; j \u003c cols; j++) {\n                if(matrix[i][j] == 0)\n                    q.add(new int[]{i,j});\n                else\n                    matrix[i][j] = -1;\n            }\n        }\n        int distance = 0;\n        int[][] moves = {{0,1}, {0,-1}, {-1,0}, {1, 0}};\n        while(!q.isEmpty()) {\n            distance++;\n            int size = q.size();\n            for(int i = 0; i \u003c size; i++) {\n                int[] pos = q.remove();\n                for(int[] move : moves) {\n                    int x = pos[0] + move[0];\n                    int y = pos[1] + move[1];\n                    if(x \u003e= 0 \u0026\u0026 x \u003c rows \u0026\u0026  y \u003e= 0 \u0026\u0026 y \u003c cols \u0026\u0026 matrix[x][y] == -1){\n                        q.add(new int[]{x,y});\n                        matrix[x][y] = distance;\n                    }\n                }\n            }\n        }\n       return matrix; \n    }\n}\n```\n\n## Implementation 2 : BFS Approach\n\n```java\nclass Solution {\n    public int[][] updateMatrix(int[][] matrix) {\n        if(matrix == null || matrix.length == 0)\n            return matrix;\n        \n        int rows = matrix.length;\n        int cols = matrix[0].length;\n        Queue\u003cint[]\u003e q = new LinkedList\u003c\u003e();\n        Set\u003cString\u003e set = new HashSet\u003c\u003e();\n        for(int i = 0; i \u003c rows; i++) {\n            for(int j = 0; j \u003c cols; j++) {\n                if(matrix[i][j] == 0) {\n                    q.add(new int[]{i,j});\n                    set.add(i+\",\"+j);\n                }\n            }\n        }\n        int[][] directions = { {0, 1}, {0, -1}, {-1, 0}, {1, 0}};\n        int step = 1;\n        while(!q.isEmpty()) {\n            int size = q.size();\n            for(int i = 0; i \u003c size; i++) {\n                int[] pos = q.remove();\n                for(int[] direction : directions) {\n                    int x = pos[0] + direction[0];\n                    int y = pos[1] + direction[1];\n                    if(x \u003e= 0 \u0026\u0026 x \u003c rows \u0026\u0026 y \u003e= 0 \u0026\u0026 y \u003c cols \u0026\u0026 matrix[x][y] == 1 \u0026\u0026 !set.contains(x+\",\"+y)) {\n                        matrix[x][y] = step;\n                        q.add(new int[]{x,y});\n                        set.add(x+\",\"+y);\n                    }\n                }\n            }\n            step++;\n        }\n       return matrix; \n    }\n}\n```\n\n## Implementation 2 : Dynamic Programming\n\n```java\nclass Solution {\n    public int[][] updateMatrix(int[][] matrix) {\n       if (matrix == null || matrix.length == 0 || matrix[0].length == 0)\n\t\t\t     return matrix;\n\n       int distanceUpperBound = matrix.length + matrix[0].length;\n\n       for (int i = 0; i \u003c matrix.length; i++) {\n           for (int j = 0; j \u003c matrix[0].length; j++) {\n\t         if (matrix[i][j] != 0) {\n\t\t\tint topCell = (i \u003e 0) ? matrix[i - 1][j] : distanceUpperBound;\n\t\t\tint leftCell = (j \u003e 0) ? matrix[i][j - 1] : distanceUpperBound;\n\t\t\tmatrix[i][j] = Math.min(topCell, leftCell) + 1;\n\t\t  }\n\t    }\n       }\n\n       for (int i = matrix.length - 1; i \u003e= 0; i--) {\n\t   for (int j = matrix[0].length - 1; j \u003e= 0; j--) {\n\t          if (matrix[i][j] != 0) {\n\t\t\t int bottomCell = (i \u003c matrix.length - 1) ? matrix[i + 1][j] : distanceUpperBound;\n\t\t\t int rightCell = (j \u003c matrix[0].length - 1) ? matrix[i][j + 1] : distanceUpperBound;\n\t\t\t matrix[i][j] = Math.min(Math.min(bottomCell, rightCell) + 1, matrix[i][j]);\n\t\t   }\n\t    }\n        }\n      \n      return matrix;\n   }\n}\n```\n\n\n# References :\nhttps://leetcode.com/problems/01-matrix/discuss/248525/Java-BFS-solution-with-comments\n\nhttps://leetcode.com/problems/01-matrix/discuss/101051/Simple-Java-solution-beat-99-(use-DP)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2F01-matrix","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2F01-matrix","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2F01-matrix/lists"}