[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:
- You may assume all numbers in the input are non-negative integers.
- The length of Profits array and Capital array will not exceed 50,000.
- 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; }};
- The more capital
W
you have now, the more maximum capital you will eventually earn. - Working on any doable project with positive
P[i] > 0
increases your capitalW
. - 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:
- At each stage, split projects into two categories:
- "doables": ones with
C[i] <= W
(storeP[i]
inpriority_queue<int> low
) - "undoables": ones with
C[i] > W
(store(C[i], P[i])
inmultiset<pair<int,int>> high
)
- "doables": ones with
- Work on most profitable project from
low
(low.top()
) first, and update capitalW += low.top()
. - Move those previous undoables from
high
to doableslow
whoseC[i] <= W
. - 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; }};
- leetcode 502. IPO
- [LeetCode]502. IPO
- LeetCode 502. IPO
- LeetCode 502. IPO
- [leetcode]502. IPO
- LeetCode 502. IPO 题解
- leetcode 502. IPO
- [Leetcode] 502. IPO 解题报告
- leetcode 502. IPO 贪心算法
- Leetcode / C++ / 502. IPO 解题报告+源代码
- LeetCode 题解(Week 5):502. IPO
- 502. IPO
- 502. IPO
- 502. IPO
- 502. IPO
- 502. IPO
- 502. IPO
- LeetCode Week 6: IPO
- HDU
- VM搭建centos系统注意事项以及配置
- React-Navigation中文文档
- JavaScript入门篇
- 8.12
- [leetcode]502. IPO
- cross-entropy for one-stage detecor
- UVA10375ChooseAndDivide
- Web安全测试之XSS
- 不忘初心向前走 2017.8.12 B
- 模仿QQ,实现列表简单折叠
- 第一个网站搭建过程额(1)
- 2018网易内推测试工程师选择题
- java语言基础——实现int类型数组元素拷贝