-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathBestMeetingSpot.cpp
36 lines (33 loc) · 1.04 KB
/
BestMeetingSpot.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Problem: https://leetcode.com/problems/best-meeting-point/
#include <vector>
using namespace std;
class BestMeetingSpot {
public:
int minTotalDistance(vector<vector<int>>& grid) {
int max_rows = grid.size();
int max_cols = grid[0].size();
vector<int> row;
vector<int> col;
row.resize(max_cols, 0);
col.resize(max_rows, 0);
for (int r = 0; r < max_rows; ++r) {
for (int c = 0; c < max_cols; ++c) {
if (grid[r][c] == 0) continue;
row[c]++; col[r]++;
}
}
return getMinDist(row) + getMinDist(col);
}
private:
int getMinDist(vector<int>& nums) {
int min_dist = INT_MAX;
for (int i = 0; i < nums.size(); ++i) {
int curr_dist = 0;
for (int j = 0; j < nums.size(); ++j) {
curr_dist += nums[j] * abs(j - i);
}
min_dist = min(min_dist, curr_dist);
}
return min_dist;
}
};