> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# IPO Python Solution with Tests

> Tested Python solution for LeetCode 502 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 502, Hard. Topics: Array, Greedy, Sorting, Heap (Priority Queue). [View on LeetCode](https://leetcode.com/problems/ipo/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 502   # by problem number
lcpy gen -s ipo   # by problem name
```

## Problem

Suppose LeetCode will start its IPO soon. To sell a good price of its shares, it can only finish at most `k` distinct projects before the IPO. Help LeetCode maximize its total capital.

You are given `n` projects where the `ith` project has a pure profit `profits[i]` and a minimum capital `capital[i]` is needed to start it.

Initially, you have `w` capital. When you finish a project, you obtain its pure profit, which is added to your total capital.

Pick a list of **at most** `k` distinct projects to **maximize your final capital**, and return the final maximized capital.

### Examples

```
Input: k = 2, w = 0, profits = [1,2,3], capital = [0,1,1]
Output: 4
Explanation: Start with capital 0, only project 0 is affordable. Finish it -> capital 1. Now projects 1 and 2 are affordable; finish project 2 -> capital 4.
```

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

### Constraints

* 1 \<= k \<= 10^5
* 0 \<= w \<= 10^9
* n == profits.length
* n == capital.length
* 1 \<= n \<= 10^5
* 0 \<= profits\[i] \<= 10^4
* 0 \<= capital\[i] \<= 10^9

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/ipo/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/ipo/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import heapq


class Solution:
    # Time: O((n + k) * log n) sorting + heap operations
    # Space: O(n) for the heap
    def find_maximized_capital(self, k: int, w: int, profits: list[int], capital: list[int]) -> int:
        # Sort projects by required capital ascending.
        projects = sorted(zip(capital, profits, strict=True))
        heap: list[int] = []  # max-heap of profits (stored negated)
        index = 0
        n = len(projects)
        current = w
        for _ in range(k):
            # Push every project now affordable into the profit heap.
            while index < n and projects[index][0] <= current:
                heapq.heappush(heap, -projects[index][1])
                index += 1
            if not heap:
                break
            current += -heapq.heappop(heap)
        return current
```

## Complexity

| Time                                          | Space             |
| --------------------------------------------- | ----------------- |
| O((n + k) \* log n) sorting + heap operations | O(n) for the heap |

## Tags

[NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
