[leetcode]502. IPO

题目链接:https://leetcode.com/problems/ipo/description/

Suppose LeetCode will start its IPO soon. In order to sell a good price of its shares to Venture Capital, LeetCode would like to work on some projects to increase its capital before the IPO. Since it has limited resources, it can only finish at most k distinct projects before the IPO. Help LeetCode design the best way to maximize its total capital after finishing at most k distinct projects.

You are given several projects. For each project i, it has a pure profit Pi and a minimum capital of Ci is needed to start the corresponding project. Initially, you have W capital. When you finish a project, you will obtain its pure profit and the profit will be added to your total capital.

To sum up, pick a list of at most k distinct projects from given projects to maximize your final capital, and output your final maximized capital.

Example 1:

Input: k=2, W=0, Profits=[1,2,3], Capital=[0,1,1].

Output: 4

Explanation: Since your initial capital is 0, you can only start the project indexed 0.
             After finishing it you will obtain profit 1 and your capital becomes 1.
             With capital 1, you can either start the project indexed 1 or the project indexed 2.
             Since you can choose at most 2 projects, you need to finish the project indexed 2 to get the maximum capital.
             Therefore, output the final maximized capital, which is 0 + 1 + 3 = 4.

Note:

  1. You may assume all numbers in the input are non-negative integers.
  2. The length of Profits array and Capital array will not exceed 50,000.
  3. The answer is guaranteed to fit in a 32-bit signed integer.
class Solution {
public:
    struct Node {int profit, capital;};

    int findMaximizedCapital(int k, int W, vector& Profits, vector& Capital) {
        if(Profits.empty() || Capital.empty()) return W;
        vector projects;
        for(int i = 0; i < Profits.size(); i++) 
            projects.push_back(new Node({Profits[i], Capital[i]}));
        multiset pq;
        int l = k;
        sort(projects.begin(), projects.end(), [&](Node* n1, Node* n2) {return n1->capital < n2->capital;});
        for(auto start = projects.begin(); k > 0; k--) {
            for(; start != projects.end() && (*start)->capital <= W; start++) {
                pq.insert((*start)->profit);
                // 此行可以注释掉
                if(pq.size() > l) pq.erase(pq.begin()); 
            } 
            if(pq.empty()) break;
            W += *pq.rbegin();
            pq.erase(prev(pq.end()));
        }
        return W;
    }
};

  1. The more capital W you have now, the more maximum capital you will eventually earn.
  2. Working on any doable project with positive P[i] > 0 increases your capital W.
  3. Any project with P[i] = 0 is useless and should be filtered away immediately (note that the problem only guarantees all inputs non-negative).

Therefore, always work on the most profitable project P[i] first as long as it is doable until we reach maximum k projects or all doable projects are done.

The algorithm will be straightforward:

  1. At each stage, split projects into two categories:
    • "doables": ones with C[i] <= W (store P[i] in priority_queue low)
    • "undoables": ones with C[i] > W (store (C[i], P[i]) in multiset> high)
  2. Work on most profitable project from low (low.top()) first, and update capital W += low.top().
  3. Move those previous undoables from high to doables low whose C[i] <= W.
  4. Repeat steps 2 and 3 until we reach maximum k projects or no more doable projects.

class Solution {
public:
    int findMaximizedCapital(int k, int W, vector& P, vector& C) {
        priority_queue low;      // P[i]'s within current W
        multiset> high; // (C[i],P[i])'s' outside current W

        for (int i = 0; i < P.size(); ++i) // initialize low and high
            if(P[i] > 0) if (C[i] <= W) low.push(P[i]); else high.emplace(C[i], P[i]);

        while (k-- && low.size()) {
            W += low.top(), low.pop(); // greedy to work on most profitable first
            for (auto i = high.begin(); high.size() && i->first <= W; i = high.erase(i)) low.push(i->second);
        }
        return W;
    }
};

你可能感兴趣的:(leetcode,贪心,leetcode,算法,职场和发展)