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.
 

Approach #1: C++.

class Solution {
public:
int findMaximizedCapital(int k, int W, vector<int>& Profits, vector<int>& Capital) {
priority_queue<int> pq;
vector<pair<int, int>> temp;
for (int i = 0; i < Capital.size(); ++i)
temp.push_back(make_pair(Capital[i], Profits[i]));
sort(temp.begin(), temp.end());
int index = 0;
while (k--) {
while (index < Capital.size() && temp[index].first <= W) {
pq.push(temp[index].second);
index++;
}
if (pq.empty()) break;
W += pq.top();
pq.pop();
}
return W;
}
};

  

Approach #2: Java.

class Solution {
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;
}
}

  

Approach #3: Python.

class Solution(object):
def findMaximizedCapital(self, k, W, Profits, Capital):
"""
:type k: int
:type W: int
:type Profits: List[int]
:type Capital: List[int]
:rtype: int
"""
heap = []
projects = sorted(zip(Profits, Capital), key=lambda l:l[1])
i = 0
for _ in range(k):
while i < len(projects) and projects[i][1] <= W:
heapq.heappush(heap, -projects[i][0])
i += 1
if heap: W -= heapq.heappop(heap)
return W

  

502. IPO的更多相关文章

  1. Java实现 LeetCode 502 IPO(LeetCode:我疯起来连自己都卖)

    502. IPO 假设 力扣(LeetCode)即将开始其 IPO.为了以更高的价格将股票卖给风险投资公司,力扣 希望在 IPO 之前开展一些项目以增加其资本. 由于资源有限,它只能在 IPO 之前完 ...

  2. [LeetCode解题报告] 502. IPO

    题目描述 假设 LeetCode 即将开始其 IPO.为了以更高的价格将股票卖给风险投资公司,LeetCode希望在 IPO 之前开展一些项目以增加其资本. 由于资源有限,它只能在 IPO 之前完成最 ...

  3. 【LeetCode】502. IPO

    题目 假设 LeetCode 即将开始其 IPO.为了以更高的价格将股票卖给风险投资公司,LeetCode希望在 IPO 之前开展一些项目以增加其资本. 由于资源有限,它只能在 IPO 之前完成最多 ...

  4. Leetcode 502.IPO

    IPO 假设 LeetCode 即将开始其 IPO.为了以更高的价格将股票卖给风险投资公司,LeetCode希望在 IPO 之前开展一些项目以增加其资本. 由于资源有限,它只能在 IPO 之前完成最多 ...

  5. 502 IPO 上市

    详见:https://leetcode.com/problems/ipo/description/ C++: class Solution { public: int findMaximizedCap ...

  6. 第九周 Leetcode 502. IPO (HARD)

    Leetcode 502 一个公司 目前有资产W 可以选择实现K个项目,每个项目要求公司当前有一定的资产,且每个项目可以使公司的总资产增加一个非负数. 项目数50000 设计一个优先队列,对于当前状态 ...

  7. [LeetCode] 502. IPO 上市

    Suppose LeetCode will start its IPO soon. In order to sell a good price of its shares to Venture Cap ...

  8. 502. IPO(最小堆+最大堆法 or 排序法)

    题目: 链接:https://leetcode-cn.com/problems/ipo/submissions/ 假设 力扣(LeetCode)即将开始其 IPO.为了以更高的价格将股票卖给风险投资公 ...

  9. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

随机推荐

  1. AngularJS:Bootstrap

    ylbtech-AngularJS:Bootstrap 1.返回顶部 1. AngularJS Bootstrap AngularJS 的首选样式表是 Twitter Bootstrap, Twitt ...

  2. 数据库:MySQL索引背后的数据结构及算法原理【转】

    原文:http://blog.codinglabs.org/articles/theory-of-mysql-index.html 摘要 本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话 ...

  3. 利用DNS进行传输数据

    曾经有这样一道题目,困了我数个小时,最后我尝试利用此方法时我知道真相的时候,眼泪已掉下来. 遇到的是一个流量分析题,分析DNS数据,拿到flag 流量如图所示: 进入linux,提取: [root@s ...

  4. 记工作的变化--入住DB

    2013年11月1日----一个值得纪念的日子! 今天才是我作为一个劳动者,步入社会的真正开始. 以前一直觉得做技术的技术做好就行了不用在意其余的细节.现实是做人(沟通)比做技术更重要! 以前一直觉得 ...

  5. 清空select标签中option选项的4种不同方式

    转自:https://blog.csdn.net/pt_sm/article/details/53521560 方法一 document.getElementById("selectid&q ...

  6. 反向索引(Inverted Index)

    转自:http://zhangyu8374.iteye.com/blog/86307 反向索引是一种索引结构,它存储了单词与单词自身在一个或多个文档中所在位置之间的映射.反向索引通常利用关联数组实现. ...

  7. oracle 基础 执行sql文件

    Oracle执行外部文件: c:>sqlplus user/pwd@db sql>@new.sql 执行多个sql文件: 1.把所有的文件都放在同一个目录下,然后在命令行里执行命令:    ...

  8. ffmpeg添加水印的方法举例 (砖)

    网上大部分关于ffmpeg加视频水印的方法还是使用vhook,在现在的ffmpeg中已经不推荐使用,但是也能编译,也能使用,至于效果,一会再说.现在的ffmpeg推荐使用的是libavfilter,但 ...

  9. BigDecimal的equals与compareTo

    equals方法的话会不仅会比较值的大小,还会比较两个对象的精确度, compareTo方法则不会比较精确度,只比较数值的大小

  10. js分页demo

    纯js实现分页   原理:所有数据已加载好,js通过遍历部分显示,实现分页效果 html代码 <html> <head> <meta charset='utf-8'> ...