8.23 - hard - 97

502. IPO

手撸成功,考虑到排序的话,可以多考虑考虑heap。

class Solution(object):
    def findMaximizedCapital(self, k, W, Profits, Capital):
        """
        :type k: int
        :type W: int
        :type Profits: List[int]
        :type Capital: List[int]
        :rtype: int
        """
        
        # init - add all project of capital v less than W into heap
        capital_heap = []
        for i in range(len(Profits)):
            heapq.heappush(capital_heap, [Capital[i], Profits[i]])
        
        heap = []
        res = 0
        while k > 0 and (capital_heap or heap):
            
            while capital_heap and capital_heap[0][0] <= W:
                c, p= heapq.heappop(capital_heap)
                heapq.heappush(heap, [-p, c])
            
            if not heap:
                break
            val, cost = heapq.heappop(heap)
            val = -val
            W = W + val
            k -= 1
        
        return W
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,904评论 25 709
  • 概述 排序有内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部...
    蚁前阅读 10,583评论 0 52
  • 概述:排序有内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部...
    每天刷两次牙阅读 9,079评论 0 15
  • 1.插入排序—直接插入排序(Straight Insertion Sort) 基本思想: 将一个记录插入到已排序好...
    依依玖玥阅读 5,013评论 0 2
  • 在有些新闻微博的下面,有很多负面的评论,说恶心的,说败类的,都有。还被很多人点赞顶到了上面。 有的人和他据理力争,...
    MarvinHu阅读 1,885评论 0 1