Skip to content

Commit f3d57c2

Browse files
committed
BestMeetingPoint296
1 parent e71c2c1 commit f3d57c2

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@
5555
| [Graph](https://github.com/fluency03/leetcode-java/blob/master/src/graph) |
5656

5757

58-
# Total: 537
58+
# Total: 538
5959

6060
| Easy | Medium | Hard | - |
6161
|:-------:|:-------:|:----:|:---:|
62-
| 142 | 292 | 91 | 12 |
62+
| 142 | 292 | 92 | 12 |
6363

6464

6565
| Question | Solution | Difficulty |
@@ -301,6 +301,7 @@
301301
| [291. Word Pattern II](https://leetcode.com/problems/word-pattern-ii/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/WordPatternII291.java) | Hard |
302302
| [293. Flip Game](https://leetcode.com/problems/flip-game/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/FlipGame293.java) | Easy |
303303
| [295. Find Median from Data Stream](https://leetcode.com/problems/find-median-from-data-stream/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/FindMedianFromDataStream295.java) | Hard |
304+
| [296. Best Meeting Point](https://leetcode.com/problems/best-meeting-point/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/BestMeetingPoint296.java) | Hard |
304305
| [297. Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/SerializeAndDeserializeBinaryTree297.java) | Hard |
305306
| [298. Binary Tree Longest Consecutive Sequence](https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/BinaryTreeLongestConsecutiveSequence298.java) | Medium |
306307
| [299. Bulls and Cows](https://leetcode.com/problems/bulls-and-cows/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/BullsAndCows299.java) | Medium |

src/BestMeetingPoint296.java

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* A group of two or more people wants to meet and minimize the total travel
3+
* distance. You are given a 2D grid of values 0 or 1, where each 1 marks the
4+
* home of someone in the group. The distance is calculated using
5+
* Manhattan Distance (http://en.wikipedia.org/wiki/Taxicab_geometry), where
6+
* distance(p1, p2) = |p2.x - p1.x| + |p2.y - p1.y|.
7+
*
8+
* Example:
9+
*
10+
* Input:
11+
* 1 - 0 - 0 - 0 - 1
12+
* | | | | |
13+
* 0 - 0 - 0 - 0 - 0
14+
* | | | | |
15+
* 0 - 0 - 1 - 0 - 0
16+
*
17+
* Output: 6
18+
*
19+
* Explanation: Given three people living at (0,0), (0,4), and (2,2):
20+
* The point (0,2) is an ideal meeting point, as the total travel distance
21+
* of 2+2+2=6 is minimal. So return 6.
22+
*/
23+
24+
public class BestMeetingPoint296 {
25+
/**
26+
* https://leetcode.com/problems/best-meeting-point/discuss/74186/14ms-java-solution
27+
*/
28+
public int minTotalDistance(int[][] grid) {
29+
int m = grid.length;
30+
int n = grid[0].length;
31+
List<Integer> I = new ArrayList<>(m);
32+
List<Integer> J = new ArrayList<>(n);
33+
for(int i = 0; i < m; i++){
34+
for(int j = 0; j < n; j++){
35+
if(grid[i][j] == 1){
36+
I.add(i);
37+
J.add(j);
38+
}
39+
}
40+
}
41+
return getMin(I) + getMin(J);
42+
}
43+
44+
private int getMin(List<Integer> list){
45+
int ret = 0;
46+
Collections.sort(list);
47+
int i = 0;
48+
int j = list.size() - 1;
49+
while(i < j){
50+
ret += list.get(j--) - list.get(i++);
51+
}
52+
return ret;
53+
}
54+
55+
}

0 commit comments

Comments
 (0)