forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_296.java
40 lines (36 loc) · 1.23 KB
/
_296.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
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class _296 {
public static class Solution1 {
/**
* Credit: https://leetcode.com/problems/best-meeting-point/solution/ Approach 3
*/
public int minTotalDistance(int[][] grid) {
int m = grid.length;
int n = grid[0].length;
List<Integer> rows = new ArrayList<>();
List<Integer> cols = new ArrayList<>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1) {
rows.add(i);
cols.add(j);
}
}
}
int rowMedian = rows.get(rows.size() / 2);
Collections.sort(cols);
int colMedian = cols.get(cols.size() / 2);
return minDistance1D(rows, rowMedian) + minDistance1D(cols, colMedian);
}
private int minDistance1D(List<Integer> points, int median) {
int distance = 0;
for (int i : points) {
distance += Math.abs(i - median);
}
return distance;
}
}
}