
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Best Meeting Point in C++
Suppose there is a group of two or more people and they wants to meet and minimize the total travel distance. We have a 2D grid of values 0 or 1, where each 1 mark the home of someone in the group. The distance is calculated using the formula of Manhattan Distance, so distance(p1, p2) = |p2.x - p1.x| + |p2.y - p1.y|.
So, if the input is like
1 | 0 | 0 | 0 | 1 |
0 | 0 | 0 | 0 | 0 |
0 | 0 | 1 | 0 | 0 |
then the output will be 6 as from the matrix we can understand that three people living at (0,0), (0,4), and (2,2): The point (0,2) is an ideal meeting point, as the total travel distance of 2+2+2=6 is minimum.
To solve this, we will follow these steps −
Define a function get(), this will take an array v,
sort the array v
i := 0
j := size of v
ret := 0
while i < j, do −
ret := ret + v[j] - v[i]
(increase i by 1)
(decrease j by 1)
return ret
From the main method do the following −
Define an array row
Define an array col
for initialize i := 0, when i < size of grid, update (increase i by 1), do −
for initialize j := 0, when j < size of grid[0], update (increase j by 1), do −
if grid[i, j] is non-zero, then −
insert i at the end of row
insert j at the end of col
return get(row) + get(col)
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; class Solution { public: int minTotalDistance(vector<vector<int>>& grid) { vector<int> row; vector<int> col; for (int i = 0; i < grid.size(); i++) { for (int j = 0; j < grid[0].size(); j++) { if (grid[i][j]) { row.push_back(i); col.push_back(j); } } } return get(row) + get(col); } int get(vector <int> v){ sort(v.begin(), v.end()); int i = 0; int j = v.size() - 1; int ret = 0; while (i < j) { ret += v[j] - v[i]; i++; j--; } return ret; } }; main(){ Solution ob; vector<vector<int>> v = {{1,0,0,0,1},{0,0,0,0,0},{0,0,1,0,0}}; cout << (ob.minTotalDistance(v)); }
Input
{{1,0,0,0,1},{0,0,0,0,0},{0,0,1,0,0}}
Output
6
- Related Articles
- Best meeting point in 2D binary array in C++
- Meeting Scheduler in C++
- What are the best ways to run a meeting?
- Meeting Rooms II in C++
- C++ code to find minimal tiredness after meeting
- Meeting Rooms 2 problem in JavaScript
- C++ program to find the best fit rectangle that covers a given point
- How to use Net Meeting and what is the code to start and join the meeting online?
- Best Sightseeing Pair in C++
- Rotation of a point about another point in C++
- Draw a quadrilateral PQRS. Draw its diagonals. Name them. Is the meeting point of the diagonals in the interior or exterior of the quadrilateral?
- Floating point comparison in C++
- 6 ways to hold a virtual group meeting
- How to create a meeting with the zoom API in Python?
- Draw a rough sketch of a quadrilateral PQRS. Draw its diagonals. Name them.Is the meeting point of the diagonals in the interior or exterior of the quadrilateral?
