296. Best Meeting Point
把兩個(gè)坐標(biāo)拆分放入rows忙厌,cols舅柜,然后求median就是中位數(shù)就是要聚會(huì)的坐標(biāo)妻献。證明在這里:https://leetcode.com/problems/best-meeting-point/discuss/
class Solution(object):
def minTotalDistance(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
rows = []
cols = []
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
rows.append(i)
cols.append(j)
rows = sorted(rows)
cols = sorted(cols)
x, y = [rows[len(rows)/2], cols[len(cols)/2]]
res = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
res += abs(i-x) + abs(j-y)
return res