502. IPO
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.
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的更多相关文章
- Java实现 LeetCode 502 IPO(LeetCode:我疯起来连自己都卖)
502. IPO 假设 力扣(LeetCode)即将开始其 IPO.为了以更高的价格将股票卖给风险投资公司,力扣 希望在 IPO 之前开展一些项目以增加其资本. 由于资源有限,它只能在 IPO 之前完 ...
- [LeetCode解题报告] 502. IPO
题目描述 假设 LeetCode 即将开始其 IPO.为了以更高的价格将股票卖给风险投资公司,LeetCode希望在 IPO 之前开展一些项目以增加其资本. 由于资源有限,它只能在 IPO 之前完成最 ...
- 【LeetCode】502. IPO
题目 假设 LeetCode 即将开始其 IPO.为了以更高的价格将股票卖给风险投资公司,LeetCode希望在 IPO 之前开展一些项目以增加其资本. 由于资源有限,它只能在 IPO 之前完成最多 ...
- Leetcode 502.IPO
IPO 假设 LeetCode 即将开始其 IPO.为了以更高的价格将股票卖给风险投资公司,LeetCode希望在 IPO 之前开展一些项目以增加其资本. 由于资源有限,它只能在 IPO 之前完成最多 ...
- 502 IPO 上市
详见:https://leetcode.com/problems/ipo/description/ C++: class Solution { public: int findMaximizedCap ...
- 第九周 Leetcode 502. IPO (HARD)
Leetcode 502 一个公司 目前有资产W 可以选择实现K个项目,每个项目要求公司当前有一定的资产,且每个项目可以使公司的总资产增加一个非负数. 项目数50000 设计一个优先队列,对于当前状态 ...
- [LeetCode] 502. IPO 上市
Suppose LeetCode will start its IPO soon. In order to sell a good price of its shares to Venture Cap ...
- 502. IPO(最小堆+最大堆法 or 排序法)
题目: 链接:https://leetcode-cn.com/problems/ipo/submissions/ 假设 力扣(LeetCode)即将开始其 IPO.为了以更高的价格将股票卖给风险投资公 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- Azure CLI的Query
Azure CLI 2.0是基于Python的命令行.其命令直观,使用非常方便. 其输出有四种模式: --output -o : Output format. Allowed values: json ...
- 机器学习:PCA(使用梯度上升法求解数据主成分 Ⅰ )
一.目标函数的梯度求解公式 PCA 降维的具体实现,转变为: 方案:梯度上升法优化效用函数,找到其最大值时对应的主成分 w : 效用函数中,向量 w 是变量: 在最终要求取降维后的数据集时,w 是参数 ...
- Java中实现MongoDB自增主键ID
1.了解MongoDB的ObjectId MongoDB的文档固定是使用“_id”作为主键的,它可以是任何类型的,默认是个ObjectId对象(在Java中则表现为字符串),那么为什么M ...
- String是基本的数据类型吗?
String不是基本的数据类型,是final修饰的java类,java中的基本类型一共有8个,它们分别为: 1 字符类型:byte,char 2 基本整型:short,int,long 3 浮点型:f ...
- python with open as f 写韩文中文乱码
python3和python2的写法不一样具体如下: python3: with open(r'd:\ssss.txt','w',encoding='utf-8') as f: f.write(u'中 ...
- 数据库学习笔记(二)MySQL数据库进阶
MySQL 进阶 关于连表 左右连表: join 上下连表: union #自动去重 (当两张表里的数据,有重复的才会自动去重) union all #不去重 #上下连表示例: select sid, ...
- 语法错误: 标识符“acosf”
1>e:\vs2010\vc\include\cmath(19): error C2061: 语法错误: 标识符“acosf” 1>e:\vs2010\vc\include\cmath(1 ...
- Firefox切换页面默认显示语言
重新安装了下Firefox,发现页面语言变为中文,而我的有些脚本是在英文界面录的,因此想把默认语言改为英文. 方法如下: 工具 - 选项 - 语言(选择...),将英文上移到顶部 在做上面的修改之前, ...
- 阿里云服务器ubuntu安装redis2.8.13
阿里云服务器ubuntu安装redis2.8.13 2014-09-04 16:14 | coding云 | 2198次阅读 | 暂无评论 一.下载redis 可以先下载到本地,然后ftp到服 ...
- byte[] 的toString() 和 new String(byte[]) 的区别
今天在Android上测试压缩和解压缩. 获得压缩后的byte[]数组后,直接用 byte[].toString()方法取得字符串. 然后用这个字符串再反向来解压缩,还原数据.却发现还原回来的字符串有 ...