leetcode 502. IPO difficulty - large root heap - small root heap - ordered list

Suppose LeetCode is about to start IPO. In order to sell shares to venture capital companies at a higher price, Li Kou hopes to carry out some projects to increase its capital before IPO. Due to limited resources, it can only complete up to k different projects before IPO. Help force buckle design the way to get the maximum total capital after completing up to k different projects.

Here are n items for you. For each project I, it has a net profit profits[i], and the minimum capital[i] required to start the project.

Initially, your capital was w. When you complete a project, you will get a net profit, and the profit will be added to your total capital.

In summary, select a list of up to k different projects from a given project to maximize the final capital and output the maximum capital available.

The answer is guaranteed to be in the range of 32-bit signed integers.

Example 1:

Input: k = 2, w = 0, profiles = [1,2,3], capital = [0,1,1]
Output: 4
Explanation:
Since your initial capital is 0, you can only start from project 0.
After completion, you will get a profit of 1 and your total capital will become 1.
At this point, you can choose to start project 1 or 2.
Since you can select up to two projects, you need to complete project 2 to get the maximum capital.
Therefore, the output of the last maximized capital is 0 + 1 + 3 = 4.
Example 2:

Input: k = 3, w = 0, profiles = [1,2,3], capital = [0,1,2]
Output: 6

Source: LeetCode
Link: https://leetcode-cn.com/problems/ipo

tips

  • Arr.sort (key = lambda x: x [0]) tuple sorting

Solution 1 large root heap - v1 pass - poor efficiency

The one with the largest profit is in the front, and the one with excessive cost is put into the candidate queue. After the profit increases, the one with excessive cost is put into the heap and added to the next screening

import heapq


class Solution:
    def findMaximizedCapital(self, k: int, w: int, profits, capital) -> int:
        ml = []
        for i,(p,c) in enumerate(zip(profits,capital)):
            heapq.heappush(ml,(0-p,c,i))
        waiting = []
        while len(ml)>0:
            p,c,i = heapq.heappop(ml)
            if c>w:
                waiting.append((p, c, i))
                continue
            w+=0-p
            k-=1
            if k==0:return w
            for wa in waiting:
                heapq.heappush(ml,wa)
            waiting = []
        return w

The effect is poor

Solution 2 - large root heap - small root heap v2 - slightly improved passing effect

Candidate queues are reduced to the root heap based on cost
Select the queue to enlarge the root heap according to the profit

As w increases, there will be fewer and fewer projects with cost constraints. Therefore, at the beginning, only items higher than W are added to the candidate queue, and items not higher than W are added to the selection queue. Select according to the profit from high to low. After one investment, w changes. The projects that can be invested in the candidate queue can be added to the selection queue.

import heapq
class Solution:
    def findMaximizedCapital(self, k: int, w: int, profits, capital) -> int:
        ml = []
        waiting = []
        for i,(p,c) in enumerate(zip(profits,capital)):
            if w <c :
                heapq.heappush(waiting,(c, p,i))
            else:heapq.heappush(ml,(0-p,c,i))
        while len(ml)>0:
            p,c,i = heapq.heappop(ml)
            w+=0-p
            k-=1
            if k==0:return w
            while(len(waiting)>0):
                c, p, i = waiting[0]
                if w<c:break
                heapq.heappop(waiting)
                heapq.heappush(ml, (0 - p, c, i))
        return w

Solution 3 large root heap - ordered list - pass - the effect is slightly better

The candidate queue has a sequence table (from small to large) according to the cost
Select the queue to enlarge the root heap according to the profit

'''
Candidate queues do not have to be maintained repeatedly with a small root heap
'''
import heapq
from sortedcontainers import SortedList
class Solution:
    def findMaximizedCapital(self, k: int, w: int, profits, capital) -> int:
        ml = []
        waiting = SortedList()
        for i,(p,c) in enumerate(zip(profits,capital)):
            if w <c :
                waiting.add((c, p,i))
            else:heapq.heappush(ml,(0-p,c,i))
        wid = 0
        while len(ml)>0:
            p,c,i = heapq.heappop(ml)
            w+=0-p
            k-=1
            if k==0:return w
            while(len(waiting)>wid):
                c, p, i = waiting[wid]
                if w<c:break
                wid+=1
                heapq.heappush(ml, (0 - p, c, i))
        return w

The effect improvement is not very obvious

Tags: Python Algorithm leetcode

Posted on Thu, 09 Sep 2021 22:02:53 -0400 by Solemn