[leetcode]502. IPO

来源:互联网 发布:网络语言6的意思 编辑:程序博客网 时间:2024/06/16 03:23

题目链接: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: 4Explanation: 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<int>& Profits, vector<int>& Capital) {        if(Profits.empty() || Capital.empty()) return W;        vector<Node*> projects;        for(int i = 0; i < Profits.size(); i++)             projects.push_back(new Node({Profits[i], Capital[i]}));        multiset<int> pq;        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() > k) 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<int> low)
    • "undoables": ones with C[i] > W (store (C[i], P[i]) in multiset<pair<int,int>> 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<int>& P, vector<int>& C) {        priority_queue<int> low;      // P[i]'s within current W        multiset<pair<int,int>> 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;    }};


原创粉丝点击