[LeetCode] 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.
这道题上来就让人眼前一亮,剑指上市,每个创业公司都有一个上市的梦想吧,博主认为照现在这种发展趋势,感觉上市并非遥不可及,资瓷一下。这道题说初始时我们的资本为0,可以交易k次,并且给了我们提供了交易所需的资本和所能获得的利润,让我们求怎样选择k次交易,使我们最终的资本最大。虽然题目中给我们的资本数组是有序的,但是OJ里的test case肯定不都是有序的,还有就是不一定需要资本大的交易利润就多,该遍历的时候还得遍历。我们可以用贪婪算法来解,每一次都选择资本范围内最大利润的进行交易,那么我们首先应该建立资本和利润对,然后根据资本的大小进行排序,然后我们根据自己当前的资本,用二分搜索法在有序数组中找第一个大于当前资本的交易的位置,然后往前退一步就是最后一个不大于当前资本的交易,然后向前遍历,找到利润最大的那个的进行交易,把利润加入资本W中,然后将这个交易对删除,这样我们就可以保证在进行k次交易后,我们的总资本最大,参见代码如下:
解法一:
class Solution {
public:
int findMaximizedCapital(int k, int W, vector<int>& Profits, vector<int>& Capital) {
vector<pair<int, int>> v;
for (int i = ; i < Capital.size(); ++i) {
v.push_back({Capital[i], Profits[i]});
}
sort(v.begin(), v.end());
for (int i = ; i < k; ++i) {
int left = , right = v.size(), mx = , idx = ;
while (left < right) {
int mid = left + (right - left) / ;
if (v[mid].first <= W) left = mid + ;
else right = mid;
}
for (int j = right - ; j >= ; --j) {
if (mx < v[j].second) {
mx = v[j].second;
idx = j;
}
}
W += mx;
v.erase(v.begin() + idx);
}
return W;
}
};
看论坛上的大神们都比较喜欢用一些可以自动排序的数据结构来做,比如我们可以使用一个最大堆和一个最小堆,把资本利润对放在最小堆中,这样需要资本小的交易就在队首,然后从队首按顺序取出资本小的交易,如果所需资本不大于当前所拥有的资本,那么就把利润资本存入最大堆中,注意这里资本和利润要翻个,因为我们希望把利润最大的交易放在队首,便于取出,这样也能实现我们的目的,参见代码如下:
解法二:
class Solution {
public:
int findMaximizedCapital(int k, int W, vector<int>& Profits, vector<int>& Capital) {
priority_queue<pair<int, int>> maxH;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> minH;
for (int i = ; i < Capital.size(); ++i) {
minH.push({Capital[i], Profits[i]});
}
for (int i = ; i < k; ++i) {
while (!minH.empty() && minH.top().first <= W) {
auto t = minH.top(); minH.pop();
maxH.push({t.second, t.first});
}
if (maxH.empty()) break;
W += maxH.top().first; maxH.pop();
}
return W;
}
};
下面这种方法跟上面的解法思路完全一样,就是数据结构有些变化,我们用multiset来模拟最小堆,然后最大堆还是用优先队列来实现,不过是需要存利润值就行了,不需要存对应的资本了,参见代码如下:
解法三:
class Solution {
public:
int findMaximizedCapital(int k, int W, vector<int>& Profits, vector<int>& Capital) {
priority_queue<int> q;
multiset<pair<int, int>> s;
for (int i = ; i < Capital.size(); ++i) {
s.insert({Capital[i], Profits[i]});
}
for (int i = ; i < k; ++i) {
for (auto it = s.begin(); it != s.end(); ++it) {
if (it->first > W) break;
q.push(it->second);
s.erase(it);
}
if (q.empty()) break;
W += q.top(); q.pop();
}
return W;
}
};
参考资料:
https://discuss.leetcode.com/topic/77768/very-simple-greedy-java-solution-using-two-priorityqueues
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] IPO 上市的更多相关文章
- [LeetCode] 502. IPO 上市
Suppose LeetCode will start its IPO soon. In order to sell a good price of its shares to Venture Cap ...
- LeetCode IPO
原题链接在这里:https://leetcode.com/problems/ipo/description/ 题目: Suppose LeetCode will start its IPO soon. ...
- 502 IPO 上市
详见:https://leetcode.com/problems/ipo/description/ C++: class Solution { public: int findMaximizedCap ...
- 从B站、爱奇艺、映客的IPO上市,看国内视频公司的内容审核现状
本文由 网易云发布. 3月30日,中央电视台<经济半小时>栏目讲述了网络上的一个顽症——色情内容.在这期主题为<互联网上的“色诱”>的节目中,央视的记者揭示了色情直播的猖獗. ...
- 从阿里巴巴IPO联想到创始人和资方关系
[小九的学堂,致力于以平凡的语言描述不平凡的技术.如要转载,请注明来源:小九的学堂.cnblogs.com/xfuture] 5月7日,阿里巴巴于今日向美国证券交易委员会(SEC)提交了IPO(首次公 ...
- 从idea到ipo
**************************************************************************************************** ...
- [转] 为什么医疗咨询服务公司Evolent Health仅用4年就华丽上市?
让医疗主体,即医院和医生担任保险角色,完全控制保费,实现医疗机构的利益最大化.美国公司EvolentHealth帮助所有医院实现这一梦想. 不觉间,已步入2015的下半年.当国内还在讨论商业保险何时能 ...
- 科技股晴间多云 阿里京东IPO或受影响
微博的时间长达一个月的时间才上市.科技股一直笼罩. Facebook一个月股价下跌21.55%:特斯拉跌幅21.69%:亚马逊的股价相比,1一个月27日高点下跌22.13%. 以前的明星股票都已进入华 ...
- 互怼、IPO、雷潮、寒冬,2018 互联网圈的那些事儿
有了人的地方,就会有江湖. 有江湖的地方,就会有门派. 有门派的地方,就会有纷争. 有纷争的地方,就会有兴衰. 2018年马上就要离我们远去了,迎接我们的将会是新的一年——2019年.在整个过去的20 ...
随机推荐
- web语义化之SEO和ARIA
在快速理解web语义化的时候,只知道web语义化有利于SEO和便于屏幕阅读器阅读,但并不知道它是如何有利于SEO和便于阅读器阅读的,带着这个疑问,进行了一番探索总结. SEO 什么是SEO? SEO( ...
- unittest自动化使用HTMLTestRunner的中文编码问题
1.使用unittest自动化测试框架,使用HTMLTestRunner生成测试报告,中文乱码问题! 如图 2.解决方法: 第一步:先在自己的测试脚本中添加 import sys reload(sys ...
- location和location.href跳转url的区别
使用 location = url 跳转,如果本地之前已经载入过该页面并有缓存,那么会直接读取本地的缓存,缓存机制是由本地浏览器设置决定的.状态码为: 200 OK (from cache) . ...
- 忘记oracle的sys密码该如何重置;附如何修改oracle数据库用户密码
参考博客:http://blog.itpub.net/26015009/viewspace-717505/ 这里只说一种方法:使用ORAPWD.EXE 工具修改密码 打开命令提示符窗口,输入如下命令: ...
- 第七次作业:Beta阶段综合报告(Java-Team)
团队:Java-Team 成员: 章辉宇(284) 吴政楠(286) 陈阳(PM:288) 韩华颂(142) 胡志权(143) github地址:https://github.com/WHUSE201 ...
- Beta敏捷冲刺每日报告——Day3
1.情况简述 Beta阶段Scrum Meeting 敏捷开发起止时间 2017.11.4 00:00 -- 2017.11.5 00:00 讨论时间地点 2017.11.4 晚9:30,电话会议会议 ...
- 团队作业7——第二次项目冲刺(Beta版本12.10)
项目每个成员的进展.存在问题.接下来两天的安排. 已完成的内容:头像功能原型设计.头像裁剪功能.头像上传功能.测试 计划完成的内容:头像功能测试.bug修复 每个人的工作 (有work item 的I ...
- Alpha冲刺第一天
Alpha冲刺第一天 站立式会议 项目进展 项目的第一天,主要工作是对项目的开发进行规划,以及将规划的成果转化为燃尽图与博客文章.依据项目需求分析报告与开题报告中已经完成的设计任务和项目规划,我们将系 ...
- 利用python处理文档中各字段出现的次数并排序
import string path = 'waldnn' with open(path,'r') as text: words = [raw_word.strip(string.punctuatio ...
- codves 3044 矩形面积求并
codves 3044 矩形面积求并 题目等级 : 钻石 Diamond 题目描述 Description 输入n个矩形,求他们总共占地面积(也就是求一下面积的并) 输入描述 Input Desc ...