Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
campus-bikes-ii.py 2.13 KB
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2019-06-03 04:22 +08:00 . Update campus-bikes-ii.py
# Time: O(w * b * 2^b)
# Space: O(b * 2^b)
# if w = b, we can even apply Hungarian algorithm (see https://en.wikipedia.org/wiki/Hungarian_algorithm),
# it can be improved to O(w^3), see https://github.com/t3nsor/codebook/blob/master/bipartite-mincost.cpp
class Solution(object): # this is slower than Solution2 in python
def assignBikes(self, workers, bikes):
"""
:type workers: List[List[int]]
:type bikes: List[List[int]]
:rtype: int
"""
def manhattan(p1, p2):
return abs(p1[0] - p2[0]) + abs(p1[1] - p2[1])
dp = [[float("inf")]*((1<<len(bikes))) for _ in xrange(2)]
dp[0][0] = 0
for i in xrange(len(workers)):
dp[(i+1)%2] = [float("inf")] * ((1<<len(bikes)))
for j in xrange(len(bikes)):
for taken in xrange((1<<len(bikes))):
if taken & (1<<j):
continue
dp[(i+1)%2][taken|(1<<j)] = \
min(dp[(i+1)%2][taken|(1<<j)],
dp[i%2][taken] +
manhattan(workers[i], bikes[j]))
return min(dp[len(workers)%2])
# Time: O((w * b * 2^b) * log(w * b * 2^b))
# Space: O(w * b * 2^b)
import heapq
class Solution2(object):
def assignBikes(self, workers, bikes):
"""
:type workers: List[List[int]]
:type bikes: List[List[int]]
:rtype: int
"""
def manhattan(p1, p2):
return abs(p1[0] - p2[0]) + abs(p1[1] - p2[1])
min_heap = [(0, 0, 0)]
lookup = set()
while min_heap:
cost, i, taken = heapq.heappop(min_heap)
if (i, taken) in lookup:
continue
lookup.add((i, taken))
if i == len(workers):
return cost
for j in xrange(len(bikes)):
if taken & (1<<j):
continue
heapq.heappush(min_heap, (cost+manhattan(workers[i], bikes[j]), # O(b)
i+1, # O(w)
taken|(1<<j))) # O(2^b)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助