[LeetCode] 502. IPO

김민우·2023년 2월 23일
0

알고리즘

목록 보기
148/189

- Problem

502. IPO

- 내 풀이 (deque, heap)

class Solution:
    def findMaximizedCapital(self, k: int, w: int, profits: List[int], capital: List[int]) -> int:
        projects = deque(sorted(zip(capital, profits)))
        max_heap = []

        for _ in range(k):
            while projects and projects[0][0] <= w:
                heappush(max_heap, -projects.popleft()[1])
            
            if max_heap:
                w -= heappop(max_heap)
        
        return w

- 결과

  • 시간 복잡도 O(N*logN + K*logN) (정렬 + heap 추가, 삭제)
  • 공간 복잡도: O(N)
profile
Pay it forward.

0개의 댓글