Tensorum
YouTubeనేర్చుకోవడం ప్రారంభించండి
LeetCode 502 · Two Heaps

IPO

LeetCode 502 · కఠినం · పరిష్కారం, intuition & దశలవారీ వాక్‌త్రూ

Sort by capital; max-heap of affordable profits.

కఠినంArrayGreedySortingHeap (Priority Queue)
LeetCode లో తెరవండి ↗

నేను IPO (LeetCode 502) ను ఇలా పరిష్కరించాను — Two Heaps ప్యాటర్న్‌లోని ఒక కఠినం ప్రాబ్లమ్. కింద: intuition, దశలవారీ approach, నా accepted Python కోడ్, animated test cases, line-by-line debugger.

కింది వివరణ ఇంగ్లీష్‌లో ఉంది.

Intuition

At any moment you can afford some projects; pick the most profitable affordable one, which raises your capital and may unlock more. Sort projects by required capital, and move newly affordable ones into a max-heap of profits each round.

Approach

  1. 1

    Pair (capital, profit) and sort.

  2. 2

    Each round, push every newly affordable project's profit.

  3. 3

    Take the largest profit; stop early if nothing is affordable.

నేను ఎలా పరిష్కరించాను

Sort + max-heapPythonO(n log n) time
class Solution:
    def findMaximizedCapital(self, k: int, w: int, profits: List[int], capital: List[int]) -> int:
        #we need to take minimal capital items first, whichever items is not available now might be available in the future
        #maximize profit
        projects = list(zip(capital,profits))
        projects.sort()
        max_heap = []
        j=0
        for i in range(k):
            while j<len(projects) and projects[j][0]<=w:
                 heapq.heappush(max_heap,-projects[j][1])
                 j+=1
            if not max_heap:
                break
            w+= -heapq.heappop(max_heap)
        return w
        
స్టెప్-త్రూ లోడ్ అవుతోంది…

IPO — తరచూ అడిగే ప్రశ్నలు

IPO వెనుక ఉన్న intuition ఏమిటి?

At any moment you can afford some projects; pick the most profitable affordable one, which raises your capital and may unlock more. Sort projects by required capital, and move newly affordable ones into a max-heap of profits each round.

IPO (LeetCode 502) ను ఎలా పరిష్కరించాలి?

1. Pair (capital, profit) and sort. 2. Each round, push every newly affordable project's profit. 3. Take the largest profit; stop early if nothing is affordable.

IPO యొక్క time, space complexity ఎంత?

Time: O(n log n + k log n). Space: O(n). (Accepted approach: Sort + max-heap.)

IPO లో సాధారణ పొరపాట్లు ఏమిటి?

Unaffordable projects aren't discarded — they may become affordable later.