class Solution:
def findMaximizedCapital(self, k: int, w: int, profits: List[int], capital: List[int]) -> int:
n = len(profits)
i = 0
arr = [(profits[i],capital[i]) for i in range(n)]
arr.sort(key=lambda x:(x[1],x[0]))
maxheap = []
for j in range(k):
while i<n and arr[i][1]<=w and j<n:
heappush(maxheap, -arr[i][0])
i +=1
if maxheap:
w -= heappop(maxheap)
else:
break
return w
arr是一个排好序的元组
i用来遍历元组的个数
python只有小根堆,所以取负数作为大根堆
贪心算法,每一步都取最大值。