本文共 638 字,大约阅读时间需要 2 分钟。
首先,将项目需要的c,p以(c,-p)形式加入heap中,让c需要小,获利大的项目靠近堆顶。随后,对比w,只要小于或等于w的c,全部弹出,放入到可做的列表中,然后选择获利最大的项目做。
class Solution: def findMaximizedCapital(self, k: int, w: int, profits: List[int], capital: List[int]) -> int: heap = [] for c,p in zip(capital, profits): heapq.heappush(heap, (c,-p)) could_do_list = [] for i in range(k): while heap and heap[0][0] <= w: c,p = heapq.heappop(heap) heapq.heappush(could_do_list, (p,c)) if could_do_list: p,c = heapq.heappop(could_do_list) w += -1 * p return w
转载地址:http://ksjii.baihongyu.com/