https://leetcode.com/problems/ipo/
https://blog.csdn.net/magicbean2/article/details/78738063
一道贪心算法的题目:每次我们都选在当前资金允许的条件下,可以获利最大的项目。所以我们首先将项目按照所需资金进行排序(需要资金越小的项目越放在前面)。然后建立一个优先队列(priority_queue)来存储目前可以做的项目,每当需要选择一个项目的时候,就从项目队列中选出在目前资金状况下可以做的项目,然后加入到优先队列中;最后在优先队列中选出获利最大的一个项目来做(位于队列首部),并更新当前资金。直到k个项目完成为止。
算法的时间复杂度是O(nlogn),空间复杂度是O(n)。
http://bookshadow.com/weblog/2017/02/05/leetcode-ipo/
X. 2 PQ
两个priorityQueue,一个capital升序排列,一个profit降序排列,用这两个找到满足当前W的最大profit
https://discuss.leetcode.com/topic/77768/very-simple-greedy-java-solution-using-two-priorityqueues
X.
https://zxi.mytechroad.com/blog/greedy/leetcode-502-ipo/
https://leetcode.com/problems/ipo/discuss/142490/Why-does-Greedy-solution-work-for-this-why-not-Dynamic-Programming
X.
Brute force (TLE)
Time complexity: O(kn)
Space complexity: O(1)
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:
- 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.
https://blog.csdn.net/magicbean2/article/details/78738063
一道贪心算法的题目:每次我们都选在当前资金允许的条件下,可以获利最大的项目。所以我们首先将项目按照所需资金进行排序(需要资金越小的项目越放在前面)。然后建立一个优先队列(priority_queue)来存储目前可以做的项目,每当需要选择一个项目的时候,就从项目队列中选出在目前资金状况下可以做的项目,然后加入到优先队列中;最后在优先队列中选出获利最大的一个项目来做(位于队列首部),并更新当前资金。直到k个项目完成为止。
算法的时间复杂度是O(nlogn),空间复杂度是O(n)。
http://bookshadow.com/weblog/2017/02/05/leetcode-ipo/
在启动资金允许的范围之内,选取收益最大的项目
public int findMaximizedCapital(int k, int W, int[] Profits, int[] Capital) {
int size = Profits.length;
int ans = W;
Point projects[] = new Point[size];
for (int i = 0; i < projects.length; i++) {
projects[i] = new Point(Capital[i], Profits[i]);
}
Arrays.sort(projects, new Comparator<Point>(){
public int compare(Point a, Point b) {
if (a.x == b.x)
return a.y - b.y;
return a.x - b.x;
}
});
PriorityQueue<Integer> pq = new PriorityQueue<Integer>(Comparator.reverseOrder());
int j = 0;
for (int i = 0; i < Math.min(k, size); i++) {
while(j < size && projects[j].x <= ans) {
pq.add(projects[j].y);
j++;
}
if (!pq.isEmpty())
ans += pq.poll();
}
return ans;
}X. 2 PQ
两个priorityQueue,一个capital升序排列,一个profit降序排列,用这两个找到满足当前W的最大profit
https://discuss.leetcode.com/topic/77768/very-simple-greedy-java-solution-using-two-priorityqueues
The idea is each time we find a project with
Algorithm:
max
profit and within current capital capability.Algorithm:
- Create (capital, profit) pairs and put them into PriorityQueue
pqCap
. This PriorityQueue sort by capital increasingly. - Keep polling pairs from
pqCap
until the project out of current capital capability. Put them into
PriorityQueuepqPro
which sort by profit decreasingly. - Poll one from
pqPro
, it's guaranteed to be the project withmax
profit and within current capital capability. Add the profit to capitalW
. - Repeat step 2 and 3 till finish
k
steps or no suitable project (pqPro.isEmpty()).
Time Complexity: For worst case, each project will be inserted and polled from both PriorityQueues once, so the overall runtime complexity should be
O(NlgN)
, N is number of projects. public int findMaximizedCapital(int k, int W, int[] Profits, int[] Capital) {
PriorityQueue<int[]> pqCap = new PriorityQueue<>((a, b) -> (a[0] - b[0]));
PriorityQueue<int[]> pqPro = new PriorityQueue<>((a, b) -> (b[1] - a[1]));
for (int i = 0; i < Profits.length; i++) {
pqCap.add(new int[] {Capital[i], Profits[i]});
}
for (int i = 0; i < k; i++) {
while (!pqCap.isEmpty() && pqCap.peek()[0] <= W) {
pqPro.add(pqCap.poll());
}
if (pqPro.isEmpty()) break;
W += pqPro.poll()[1];
}
return W;
}
Priority Queue,保持当前可能的最大利润。 一旦达到所需资金就插入可能的利润。X.
https://zxi.mytechroad.com/blog/greedy/leetcode-502-ipo/
Use priority queue and multiset to track doable and undoable projects at given W.
Time complexity: O(nlogn)
int findMaximizedCapital(int k, int W, vector<int>& Profits, vector<int>& Capital) {
priority_queue<int> doable; // sorted by profit high to low.
multiset<pair<int, int>> undoable; // {capital, profit}
for (int i = 0; i < Profits.size(); ++i) {
if (Profits[i] <= 0) continue;
if (Capital[i] <= W)
doable.push(Profits[i]);
else
undoable.emplace(Capital[i], Profits[i]);
}
auto it = undoable.cbegin();
while (!doable.empty() && k--) {
W += doable.top(); doable.pop();
while (it != undoable.cend() && it->first <= W)
doable.push(it++->second);
}
return W;
}
X.
Brute force (TLE)
Time complexity: O(kn)
Space complexity: O(1)
int findMaximizedCapital(int k, int W, vector<int>& Profits, vector<int>& Capital) {
for (int i = 0; i < k; ++i) {
int best_j = -1;
int best_profit = 0;
for (int j = 0; j < Profits.size(); ++j) {
if (Capital[j] <= W && Profits[j] > best_profit) {
best_j = j;
best_profit = Profits[j];
}
}
if (best_profit == 0) break;
W += Profits[best_j];
Profits[best_j] = INT_MIN; // Used.
}
return W;
}