../ Competitive Programming
-rw-r--r--aguang1.0 KiB1 min read

Leet Code Daily 2024-06-15 - IPO

Solution

Greedy, always take current available project with maximum profit.

Code

c++
class Solution {
public:
    int findMaximizedCapital(int k, int w, vector<int>& profits,
                             vector<int>& capital) {
        const int n = profits.size();
        vector<pair<int, int>> projects;
        for (int i = 0; i < n; i++) {
            projects.push_back(make_pair(profits[i], capital[i]));
        }
        sort(projects.begin(), projects.end(),
             [](auto& x, auto& y) { return x.second < y.second; });
        priority_queue<int> available_profits;
        for (int i = 0, j = 0; i < k; i++) {
            while (j < n && projects[j].second <= w) {
                available_profits.push(projects[j++].first);
            }
            if (available_profits.empty()) {
                break;
            }
            w += available_profits.top();
            available_profits.pop();
        }
        return w;
    }
};

COMMAND INTERFACE

↑ ↓ navigate · Enter execute · Esc exit · try “whoami”

Loading filesystem…

:help aguang.xyz

A personal website disguised as an operating system.

Ctrl / Cmd + K
Command palette + full-text search
Tab / Shift + Tab
Move between links and controls
Enter
Open a focused link or search result
↑ / ↓ in search
Navigate search results
In a directory filter
Enter/Done or ↓ focuses a result; Esc clears the filter
Esc
Clear a search or close a window