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:

  1. Input: k=2, W=0, Profits=[1,2,3], Capital=[0,1,1].
  2.  
  3. Output: 4
  4.  
  5. Explanation: Since your initial capital is 0, you can only start the project indexed 0.
  6. After finishing it you will obtain profit 1 and your capital becomes 1.
  7. With capital 1, you can either start the project indexed 1 or the project indexed 2.
  8. Since you can choose at most 2 projects, you need to finish the project indexed 2 to get the maximum capital.
  9. 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.

这道题上来就让人眼前一亮,剑指上市,每个创业公司都有一个上市的梦想吧,博主认为照现在这种发展趋势,感觉上市并非遥不可及,资瓷一下。这道题说初始时我们的资本为0,可以交易k次,并且给了我们提供了交易所需的资本和所能获得的利润,让我们求怎样选择k次交易,使我们最终的资本最大。虽然题目中给我们的资本数组是有序的,但是OJ里的test case肯定不都是有序的,还有就是不一定需要资本大的交易利润就多,该遍历的时候还得遍历。我们可以用贪婪算法来解,每一次都选择资本范围内最大利润的进行交易,那么我们首先应该建立资本和利润对,然后根据资本的大小进行排序,然后我们根据自己当前的资本,用二分搜索法在有序数组中找第一个大于当前资本的交易的位置,然后往前退一步就是最后一个不大于当前资本的交易,然后向前遍历,找到利润最大的那个的进行交易,把利润加入资本W中,然后将这个交易对删除,这样我们就可以保证在进行k次交易后,我们的总资本最大,参见代码如下:

解法一:

  1. class Solution {
  2. public:
  3. int findMaximizedCapital(int k, int W, vector<int>& Profits, vector<int>& Capital) {
  4. vector<pair<int, int>> v;
  5. for (int i = ; i < Capital.size(); ++i) {
  6. v.push_back({Capital[i], Profits[i]});
  7. }
  8. sort(v.begin(), v.end());
  9. for (int i = ; i < k; ++i) {
  10. int left = , right = v.size(), mx = , idx = ;
  11. while (left < right) {
  12. int mid = left + (right - left) / ;
  13. if (v[mid].first <= W) left = mid + ;
  14. else right = mid;
  15. }
  16. for (int j = right - ; j >= ; --j) {
  17. if (mx < v[j].second) {
  18. mx = v[j].second;
  19. idx = j;
  20. }
  21. }
  22. W += mx;
  23. v.erase(v.begin() + idx);
  24. }
  25. return W;
  26. }
  27. };

看论坛上的大神们都比较喜欢用一些可以自动排序的数据结构来做,比如我们可以使用一个最大堆和一个最小堆,把资本利润对放在最小堆中,这样需要资本小的交易就在队首,然后从队首按顺序取出资本小的交易,如果所需资本不大于当前所拥有的资本,那么就把利润资本存入最大堆中,注意这里资本和利润要翻个,因为我们希望把利润最大的交易放在队首,便于取出,这样也能实现我们的目的,参见代码如下:

解法二:

  1. class Solution {
  2. public:
  3. int findMaximizedCapital(int k, int W, vector<int>& Profits, vector<int>& Capital) {
  4. priority_queue<pair<int, int>> maxH;
  5. priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> minH;
  6. for (int i = ; i < Capital.size(); ++i) {
  7. minH.push({Capital[i], Profits[i]});
  8. }
  9. for (int i = ; i < k; ++i) {
  10. while (!minH.empty() && minH.top().first <= W) {
  11. auto t = minH.top(); minH.pop();
  12. maxH.push({t.second, t.first});
  13. }
  14. if (maxH.empty()) break;
  15. W += maxH.top().first; maxH.pop();
  16. }
  17. return W;
  18. }
  19. };

下面这种方法跟上面的解法思路完全一样,就是数据结构有些变化,我们用multiset来模拟最小堆,然后最大堆还是用优先队列来实现,不过是需要存利润值就行了,不需要存对应的资本了,参见代码如下:

解法三:

  1. class Solution {
  2. public:
  3. int findMaximizedCapital(int k, int W, vector<int>& Profits, vector<int>& Capital) {
  4. priority_queue<int> q;
  5. multiset<pair<int, int>> s;
  6. for (int i = ; i < Capital.size(); ++i) {
  7. s.insert({Capital[i], Profits[i]});
  8. }
  9. for (int i = ; i < k; ++i) {
  10. for (auto it = s.begin(); it != s.end(); ++it) {
  11. if (it->first > W) break;
  12. q.push(it->second);
  13. s.erase(it);
  14. }
  15. if (q.empty()) break;
  16. W += q.top(); q.pop();
  17. }
  18. return W;
  19. }
  20. };

参考资料:

https://discuss.leetcode.com/topic/77768/very-simple-greedy-java-solution-using-two-priorityqueues

https://discuss.leetcode.com/topic/78574/8-liner-c-42ms-beat-98-greedy-algorithm-detailed-explanation

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] IPO 上市的更多相关文章

  1. [LeetCode] 502. IPO 上市

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

  2. LeetCode IPO

    原题链接在这里:https://leetcode.com/problems/ipo/description/ 题目: Suppose LeetCode will start its IPO soon. ...

  3. 502 IPO 上市

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

  4. 从B站、爱奇艺、映客的IPO上市,看国内视频公司的内容审核现状

    本文由  网易云发布. 3月30日,中央电视台<经济半小时>栏目讲述了网络上的一个顽症——色情内容.在这期主题为<互联网上的“色诱”>的节目中,央视的记者揭示了色情直播的猖獗. ...

  5. 从阿里巴巴IPO联想到创始人和资方关系

    [小九的学堂,致力于以平凡的语言描述不平凡的技术.如要转载,请注明来源:小九的学堂.cnblogs.com/xfuture] 5月7日,阿里巴巴于今日向美国证券交易委员会(SEC)提交了IPO(首次公 ...

  6. 从idea到ipo

    **************************************************************************************************** ...

  7. [转] 为什么医疗咨询服务公司Evolent Health仅用4年就华丽上市?

    让医疗主体,即医院和医生担任保险角色,完全控制保费,实现医疗机构的利益最大化.美国公司EvolentHealth帮助所有医院实现这一梦想. 不觉间,已步入2015的下半年.当国内还在讨论商业保险何时能 ...

  8. 科技股晴间多云 阿里京东IPO或受影响

    微博的时间长达一个月的时间才上市.科技股一直笼罩. Facebook一个月股价下跌21.55%:特斯拉跌幅21.69%:亚马逊的股价相比,1一个月27日高点下跌22.13%. 以前的明星股票都已进入华 ...

  9. 互怼、IPO、雷潮、寒冬,2018 互联网圈的那些事儿

    有了人的地方,就会有江湖. 有江湖的地方,就会有门派. 有门派的地方,就会有纷争. 有纷争的地方,就会有兴衰. 2018年马上就要离我们远去了,迎接我们的将会是新的一年——2019年.在整个过去的20 ...

随机推荐

  1. [poj2185]Milking Grid_KMP

    Milking Grid poj-2185 题目大意:给出一个字符矩阵,求最小覆盖矩阵(可以残余). 注释:$1\le R\le 10^5$,$1\le C \le 75$ 想法:和bzoj1355不 ...

  2. 如何从RxJava升级到RxJava2

    如何从RxJava升级到RxJava2. RxJava2已经推出有一年半的时间,由于之前RxJava已经在现有项目中广泛使用,而RxJava2在除了很多命名外并没有太多革新,所以相信有很多人跟我一样都 ...

  3. JavaScript(第二十天)【DOM操作表格及样式】

    DOM在操作生成HTML上,还是比较简明的.不过,由于浏览器总是存在兼容和陷阱,导致最终的操作就不是那么简单方便了.本章主要了解一下DOM操作表格和样式的一些知识. 一.操作表格 <table& ...

  4. 2018上C语言程序设计(高级)作业- 初步计划

    C语言程序设计(高级)36学时,每周4学时,共9周.主要学习指针.结构和文件三部分内容.整个课程作业计划如下: PTA和博客的使用指南 若第一次使用PTA和博客,请务必先把PTA的使用简介和教师如何在 ...

  5. GNU/Hurd笔记整理

    patch 0 关于文件锁支持的解决方案,大部分是由Neal Walfield在2001年完成的.这些补丁由Marcus Brinkmann发表,随后被Michael Banck于2002年修改了部分 ...

  6. Tornado websocket应用

    应用场景 WebSocket 的特点如下 适合服务器主动推送的场景(好友上线,即时聊天信息,火灾警告,股票涨停等) 相对于Ajax和Long poll等轮询技术,它更高效,不耗费网络带宽和计算资源 它 ...

  7. webview缓存及跳转时截取url地址、监听页面变化

    缓存及一些设定 我在做一些项目时,h5做的项目手机浏览器能使用,但是在搬到webview时候不能用,这个时候通过查阅资料,原来是webview没有设定好,包括缓存.缓存大小及路径等等 mWebview ...

  8. 申请JetBrains学生免费注册码

    1.申请.edu.*后缀的邮箱 从某个知乎用户上面得到了两个可以申请的后缀edu的邮箱 上海交通大学校友统一身份认证:https://register.alumni.sjtu.edu.cn/alumn ...

  9. 通过URL传递PDF名称参数显示PDF

    1 <%@ page language="java" import="java.util.*,java.io.*" 2 pageEncoding=&quo ...

  10. 使用JDBC中的出现的乱码和查询无结果问题

    使用JDBC中的问题 连接的后出现查询结果是乱码. 1.可能是代码的编码与数据库的编码不同 ​ 有可以将二者都设置为UTF-8 2.如果比较懒得话可以只设代码为UTF-8 mysql 连接url中us ...