Thanks to visit codestin.com Credit goes to github.com
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 11153bd commit b3f1573Copy full SHA for b3f1573
0296.最佳的碰头地点/0296-最佳的碰头地点.py
@@ -0,0 +1,29 @@
1
+class Solution(object):
2
+ def minTotalDistance(self, grid):
3
+ """
4
+ :type grid: List[List[int]]
5
+ :rtype: int
6
7
+ if not grid or not grid[0]:
8
+ return -1
9
+
10
+ m, n = len(grid), len(grid[0])
11
+ row, col = [], []
12
+ for i in range(m):
13
+ for j in range(n):
14
+ if grid[i][j]:
15
+ row.append(i)
16
+ col.append(j)
17
+ meet_point = [self.findMedian(row), self.findMedian(col)]
18
19
+ res = 0
20
21
22
23
+ res += abs(i - meet_point[0]) + abs(j - meet_point[1])
24
+ return res
25
26
27
+ def findMedian(self, nums):
28
+ nums.sort()
29
+ return nums[len(nums) // 2]
0 commit comments