Best meeting point in 2D binary array
You are given a 2D grid of values 0 or 1, where each 1 marks the home of someone in a group. And the group of two or more people wants to meet and minimize the total travel distance. They can meet anywhere means that there might be a home or not.
- The distance is calculated using Manhattan Distance, where distance(p1, p2) = |p2.x – p1.x| + |p2.y – p1.y|.
- Find the total distance that needs to be traveled to reach the best meeting point (Total distance traveled is minimum).
Examples:
Input : grid[][] = {{1, 0, 0, 0, 1},
{0, 0, 0, 0, 0},
{0, 0, 1, 0, 0}};
Output : 6
Best meeting point is (0, 2).
Total distance traveled is 2 + 2 + 2 = 6
Input : grid[3][5] = {{1, 0, 1, 0, 1},
{0, 1, 0, 0, 0},
{0, 1, 1, 0, 0}};
Output : 11
Concept :-
Let the group of people be present at the coordinates G : (Gx, Gy), H : (Hx, Hy), J : (Jx, Jy), and K : (Kx, Ky).
Let the Best Meeting Point be P : (x, y)
Distance of every point from P will be given by,
D = |Gx - x| + |Gy - y| + |Hx - x| + |Hy - y| + |Jx - x| + |Jy - y| + |Kx - x| + |Ky - y|
which can also be written as,
D = Dx + Dy
where, Dx = |Gx - x| + |Hx - x| + |Jx - x| + |Kx - x|
and, Dy = |Gy - y| + |Hy - y| + |Jy - y| + |Ky - y|
To minimize D, we should minimize Dx and Dy.
Dx will be minimum if x is the median of (Gx, Hx, Jx, Kx)
and Similarly, Dy will be minimum if y is the median of (Gy, Hy, Jy, Ky)
Steps :-
- Store all horizontal and vertical positions of all group member.
- Now sort it to find minimum middle position, which will be the best meeting point.
- Find the distance of all members from best meeting point.
For example in above diagram, horizontal positions are {0, 2, 0} and vertical positions are {0, 2, 4}. After sorting both, we get {0, 0, 2} and {0, 2, 4}. Middle point is (0, 2).
Note : Even no. of 1’s have two middle points, then also it works. Two middle points means it have two best meeting points always. Both cases will give same distance. So we will consider only one best meeting point to avoid the more overhead, Because our aim is to find the distance only.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
#define ROW 3
#define COL 5
int minTotalDistance( int grid[][COL]) {
if (ROW == 0 || COL == 0)
return 0;
vector< int > vertical;
vector< int > horizontal;
for ( int i = 0; i < ROW; i++) {
for ( int j = 0; j < COL; j++) {
if (grid[i][j] == 1) {
vertical.push_back(i);
horizontal.push_back(j);
}
}
}
sort(vertical.begin(),vertical.end());
sort(horizontal.begin(),horizontal.end());
int size = vertical.size()/2;
int x = vertical[size];
int y = horizontal[size];
int distance = 0;
for ( int i = 0; i < ROW; i++)
for ( int j = 0; j < COL; j++)
if (grid[i][j] == 1)
distance += abs (x - i) + abs (y - j);
return distance;
}
int main() {
int grid[ROW][COL] = {{1, 0, 1, 0, 1}, {0, 1, 0, 0, 0},{0, 1, 1, 0, 0}};
cout << minTotalDistance(grid);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int minTotalDistance( int grid[][], int ROW , int COL)
{
if (ROW == 0 || COL == 0 )
return 0 ;
List<Integer> vertical = new ArrayList<>();
List<Integer> horizontal = new ArrayList<>();
for ( int i = 0 ; i < ROW; i++)
{
for ( int j = 0 ; j < COL; j++)
{
if (grid[i][j] == 1 )
{
vertical.add(i);
horizontal.add(j);
}
}
}
Collections.sort(vertical);
Collections.sort(horizontal);
int size = vertical.size() / 2 ;
int x = vertical.get(size);
int y = horizontal.get(size);
int distance = 0 ;
for ( int i = 0 ; i < ROW; i++)
for ( int j = 0 ; j < COL; j++)
if (grid[i][j] == 1 )
distance += Math.abs(x - i) +
Math.abs(y - j);
return distance;
}
public static void main(String[] args)
{
int grid[][] = {{ 1 , 0 , 1 , 0 , 1 },
{ 0 , 1 , 0 , 0 , 0 },
{ 0 , 1 , 1 , 0 , 0 }};
System.out.println(minTotalDistance(grid , grid.length , grid[ 0 ].length));
}
}
|
Python3
ROW = 3
COL = 5
def minTotalDistance(grid: list ) - > int :
if ROW = = 0 or COL = = 0 :
return 0
vertical = []
horizontal = []
for i in range (ROW):
for j in range (COL):
if grid[i][j] = = 1 :
vertical.append(i)
horizontal.append(j)
vertical.sort()
horizontal.sort()
size = len (vertical) / / 2
x = vertical[size]
y = horizontal[size]
distance = 0
for i in range (ROW):
for j in range (COL):
if grid[i][j] = = 1 :
distance + = abs (x - i) + abs (y - j)
return distance
if __name__ = = "__main__" :
grid = [[ 1 , 0 , 1 , 0 , 1 ],
[ 0 , 1 , 0 , 0 , 0 ],
[ 0 , 1 , 1 , 0 , 0 ]]
print (minTotalDistance(grid))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int ROW = 3;
static int COL = 5 ;
static int minTotalDistance( int [,]grid)
{
if (ROW == 0 || COL == 0)
return 0;
List< int > vertical = new List< int >();
List< int > horizontal = new List< int >();
for ( int i = 0; i < ROW; i++)
{
for ( int j = 0; j < COL; j++)
{
if (grid[i, j] == 1)
{
vertical.Add(i);
horizontal.Add(j);
}
}
}
vertical.Sort();
horizontal.Sort();
int size = vertical.Count / 2;
int x = vertical[size];
int y = horizontal[size];
int distance = 0;
for ( int i = 0; i < ROW; i++)
for ( int j = 0; j < COL; j++)
if (grid[i, j] == 1)
distance += Math.Abs(x - i) +
Math.Abs(y - j);
return distance;
}
public static void Main(String[] args)
{
int [,]grid = {{1, 0, 1, 0, 1},
{0, 1, 0, 0, 0},
{0, 1, 1, 0, 0}};
Console.WriteLine(minTotalDistance(grid));
}
}
|
Javascript
In worst case if all the elements of Matrix are set to 1. Then we would have to sort N*M elements. Time to sort is (N*M)log(N*M) or 2N^2log(N)
Time Complexity : O(M*N)
Auxiliary Space : O(N)
Last Updated :
18 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...