-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbestMeetingPoint.cpp
More file actions
92 lines (87 loc) · 2.36 KB
/
Copy pathbestMeetingPoint.cpp
File metadata and controls
92 lines (87 loc) · 2.36 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
Description
A group of two or more people wants to meet and minimize the total travel distance. You are given a 2D grid of values 0 or 1, where each 1 marks the home of someone in the group. The distance is calculated using Manhattan Distance, where distance(p1, p2) = |p2.x - p1.x| + |p2.y - p1.y|.
Example
Example 1:
Input:
[[1,0,0,0,1],[0,0,0,0,0],[0,0,1,0,0]]
Output:
6
Explanation:
The point `(0,2)` is an ideal meeting point, as the total travel distance of `2 + 2 + 2 = 6` is minimal. So return `6`.
Example 2:
Input:
[[1,1,0,0,1],[1,0,1,0,0],[0,0,1,0,1]]
Output:
14
*/
class Solution {
public:
/**
* @param grid: a 2D grid
* @return: the minimize travel distance
*/
int minTotalDistance(vector<vector<int>> &grid) {
// Write your code here
vector<pair<int, int>>points;
for (int i = 0; i < grid.size(); i++) {
for (int j = 0; j < grid[i].size(); j++) {
if (grid[i][j]) points.push_back({i,j});
}
}
int sum, ans = INT_MAX;
for (int i = 0; i < grid.size(); i++) {
for (int j = 0; j < grid[i].size(); j++) {
sum = 0;
for (int k = 0; k < points.size(); k++) {
sum+=(abs(i-points[k].first)+abs(j-points[k].second));
}
ans = min(ans, sum);
}
}
return ans;
}
};
/**
* Definition for a point.
* struct Point {
* int x;
* int y;
* Point() : x(0), y(0) {}
* Point(int a, int b) : x(a), y(b) {}
* };
*/
class Solution {
public:
/**
* @param points an array of point
* @return an integer
*/
int maxPoints(vector<Point>& points) {
// Write your code here
unordered_map<float,int> mp;
int maxNum = 0;
for(int i = 0; i < points.size(); i++)
{
mp.clear();
mp[INT_MIN] = 0;
int duplicate = 1;
for(int j = 0; j < points.size(); j++)
{
if(j == i) continue;
if(points[i].x == points[j].x && points[i].y == points[j].y)
{
duplicate++;
continue;
}
float k = points[i].x == points[j].x ? INT_MAX : (float)(points[j].y - points[i].y)/(points[j].x - points[i].x);
mp[k]++;
}
unordered_map<float, int>::iterator it = mp.begin();
for(; it != mp.end(); it++)
if(it->second + duplicate > maxNum)
maxNum = it->second + duplicate;
}
return maxNum;
}
};