{"id":22513971,"url":"https://github.com/emahtab/best-meeting-point","last_synced_at":"2026-01-06T18:08:28.504Z","repository":{"id":79525324,"uuid":"415481618","full_name":"eMahtab/best-meeting-point","owner":"eMahtab","description":"Best Meeting Point","archived":false,"fork":false,"pushed_at":"2021-10-13T03:56:53.000Z","size":41,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-02T03:26:15.919Z","etag":null,"topics":["leetcode","median"],"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":"2021-10-10T04:03:52.000Z","updated_at":"2021-10-13T03:56:55.000Z","dependencies_parsed_at":"2023-05-10T17:00:30.423Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/best-meeting-point","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fbest-meeting-point","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fbest-meeting-point/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fbest-meeting-point/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fbest-meeting-point/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/best-meeting-point/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245952331,"owners_count":20699471,"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":["leetcode","median"],"created_at":"2024-12-07T03:15:18.593Z","updated_at":"2026-01-06T18:08:28.479Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Best Meeting Point\nGiven an m x n binary grid grid where each 1 marks the home of one friend, return the minimal total travel distance.\n\nThe total travel distance is the sum of the distances between the houses of the friends and the meeting point.\n\nThe distance is calculated using Manhattan Distance, where distance(p1, p2) = |p2.x - p1.x| + |p2.y - p1.y|.\n\n![\"Best Meeting Point\"](example.JPG?raw=true)\n\n```\nConstraints:\n\n1. m == grid.length\n2. n == grid[i].length\n3. 1 \u003c= m, n \u003c= 200\n4. grid[i][j] is either 0 or 1.\n5. There will be at least two friends in the grid.\n```\n\n## 💥💥💥 : Note that, meeting point can be either a house(1 cell) or 0 cell. \n\n# Implementation 1 : Naive : Time Limit Exceeded\nFind out the distance from each cell of the grid and return the minimum distance.\n\n```java\nclass Solution {\n    public int minTotalDistance(int[][] grid) {\n        if(grid == null || grid.length == 0)\n            return 0;\n        int rows = grid.length;\n        int cols = grid[0].length;\n        List\u003cint[]\u003e houses = new ArrayList\u003c\u003e();\n        for(int i = 0; i \u003c rows; i++) {\n            for(int j = 0; j \u003c cols; j++) {\n                if(grid[i][j] == 1) {\n                    houses.add(new int[]{i,j});\n                }\n            }\n        }\n        int minDistance = Integer.MAX_VALUE;\n        for(int i = 0; i \u003c rows; i++) {\n            for(int j= 0; j \u003c cols; j++) {\n                int distance = getDistanceFromHouses(grid, i, j, houses);\n                minDistance = Math.min(minDistance, distance);\n            }\n        }\n        return minDistance;\n    }\n    \n    public int getDistanceFromHouses(int[][] grid, int row, int col, List\u003cint[]\u003e houses) {\n        int totalDistance = 0;\n        for(int[] house : houses) {\n            int distance = Math.abs(row - house[0]) + Math.abs(col - house[1]);\n            totalDistance += distance;\n        }\n        return totalDistance;\n    }\n}\n```\n\n# Implementation 2 : Calculate distance from Median\nTo find the median cell, we need to arrange the coordinates of the houses in ascending order for both x and y direction.\n```java\nclass Solution {\n    public int minTotalDistance(int[][] grid) {\n        if(grid == null || grid.length == 0)\n            return 0;\n        int rows = grid.length;\n        int cols = grid[0].length;\n        List\u003cInteger\u003e x = new ArrayList\u003c\u003e();\n        List\u003cInteger\u003e y = new ArrayList\u003c\u003e();\n        // collect x coordinates of houses in ascending order\n        for(int i = 0; i \u003c rows; i++) {\n            for(int j = 0; j \u003c cols; j++) {\n                if(grid[i][j] == 1)\n                    x.add(i);\n            }\n        }\n        // collect y coordinates of houses in ascending order\n        for(int j = 0; j \u003c cols; j++) {\n            for(int i = 0; i \u003c rows; i++) {\n                if(grid[i][j] == 1)\n                    y.add(j);\n            }\n        }\n        int xc = x.get(x.size() / 2);\n        int yc = y.get(y.size() / 2);\n        // now calculate the distance from  Median (xc,yc)\n        int minDistance = 0;\n        for(int i = 0; i \u003c rows; i++) {\n            for(int j = 0; j \u003c cols; j++) {\n                if(grid[i][j] == 1) {\n                    minDistance += Math.abs(i - xc) + Math.abs(j - yc);\n                }\n            }\n        }\n        \n        return minDistance;\n    }\n}\n```\n\n### Complexity analysis\n```\nTime complexity : O(mn), where m is number of rows and n is number of columns.\n\nSpace complexity : O(mn), where m is number of rows and n is number of columns.\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fbest-meeting-point","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fbest-meeting-point","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fbest-meeting-point/lists"}