class Solution(object):
# def minTotalDistance(self, grid):
# """
# :type grid: List[List[int]]
# :rtype: int
# """
# # sort
# 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)
# row = rows[len(rows) / 2]
# cols.sort()
# col = cols[len(cols) / 2]
# return self.minDistance1D(rows, row) + self.minDistance1D(cols, col)
# def minDistance1D(self, points, origin):
# distance = 0
# for point in points:
# distance += abs(point - origin)
# return distance
def minDistance1D(self, points):
# two points
distance = 0
i, j = 0, len(points) - 1
while i < j:
distance += points[j] - points[i]
i += 1
j -= 1
return distance
def minTotalDistance(self, grid):
rows = self.collectRows(grid)
cols = self.collectCols(grid)
row = rows[len(rows) / 2]
col = cols[len(cols) / 2]
return self.minDistance1D(rows) + self.minDistance1D(cols)
def collectRows(self, grid):
rows = []
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
rows.append(i)
return rows
def collectCols(self, grid):
cols = []
for j in range(len(grid[0])):
for i in range(len(grid)):
if grid[i][j] == 1:
cols.append(j)
return cols
python-leetcode面试题解之第296题最佳的碰头地点.zip
需积分: 1 58 浏览量
2024-06-20
04:59:27
上传
评论
收藏 1024B ZIP 举报
DdddJMs__135
- 粉丝: 1502
- 资源: 396
最新资源
- 发多少不规范都不舍得刚发巴萨刚发好吧刚发
- 惠普 HPIP230.2015-0827.81.iso
- DS18B20应用程序实例分析.pdf
- 目前DC/DC变流器的主要拓扑simulink仿真
- 计算机网络基础知识总结
- 基于人脸识别安全管理系统的设计与实现论文-参考模板
- Proteus-74HC595-SMG.zip,Proteus工程+KeilC51
- 海信电视刷机 LED48T1A(0000) 通用 LED48K370(0011) 生产用软件数据 务必确认机编一致 USB升级
- 不给机会一决胜负热一体机叶然
- 基于SpringBoot+Vue前后端分离的Java开发框架实现的问卷调查平台/问卷调查系统(源码+数据库)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈