forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBestMeetingPoint.java
141 lines (123 loc) · 3.83 KB
/
BestMeetingPoint.java
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.leetcode.math;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Level: Hard
* Link: https://leetcode.com/problems/best-meeting-point/
* 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:
*
* Input:
*
* 1 - 0 - 0 - 0 - 1
* | | | | |
* 0 - 0 - 0 - 0 - 0
* | | | | |
* 0 - 0 - 1 - 0 - 0
*
* Output: 6
*
* Explanation: Given 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 minimal. So, return 6.
*
* @author rampatra
* @since 2019-08-07
*/
public class BestMeetingPoint {
/**
* Time Complexity: O(k * i * j)
* Space Complexity: O(1)
* where,
* k = no of homes
* i = rows in grid
* j = columns in grid
*
* So, if i = j = k then you can see that it has a O(n^3) time complexity.
*
* @param grid
* @return
*/
public static int minTotalDistanceBrutForce(int[][] grid) {
int minDistance = Integer.MAX_VALUE;
List<List<Integer>> homeCoordinates = new ArrayList<>();
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == 1) {
homeCoordinates.add(Arrays.asList(i, j));
}
}
}
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
int distance = 0;
for (int k = 0; k < homeCoordinates.size(); k++) {
distance += Math.abs(homeCoordinates.get(k).get(0) - i) + Math.abs(homeCoordinates.get(k).get(1) - j);
}
minDistance = Math.min(minDistance, distance);
}
}
return minDistance;
}
public static int minTotalDistance(int[][] grid) {
return -1; // todo
}
public static void main(String[] args) {
assertEquals(6, minTotalDistanceBrutForce(new int[][]{
{1,0,0,0,1},
{0,0,0,0,0},
{0,0,1,0,0}
}));
assertEquals(4, minTotalDistanceBrutForce(new int[][]{
{1,0,0,0,1},
{0,0,0,0,0},
{0,0,0,0,0}
}));
assertEquals(1, minTotalDistanceBrutForce(new int[][]{
{1,1,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0}
}));
assertEquals(0, minTotalDistanceBrutForce(new int[][]{
{1,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0}
}));
assertEquals(0, minTotalDistanceBrutForce(new int[][]{
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0}
}));
assertEquals(6, minTotalDistance(new int[][]{
{1,0,0,0,1},
{0,0,0,0,0},
{0,0,1,0,0}
}));
assertEquals(4, minTotalDistance(new int[][]{
{1,0,0,0,1},
{0,0,0,0,0},
{0,0,0,0,0}
}));
assertEquals(1, minTotalDistance(new int[][]{
{1,1,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0}
}));
assertEquals(0, minTotalDistance(new int[][]{
{1,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0}
}));
assertEquals(0, minTotalDistance(new int[][]{
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0}
}));
}
}