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.