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 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- 使用Jersey和Jetty开发RESTful Web service
Jersey RESTful 框架是开源的RESTful框架, 实现了JAX-RS (JSR 311 & JSR 339) 规范,是JAX-RS的参考实现,并且提供了更多的特性和工具,简化了R ...
- 什么是VBA,他有什么作用
目录 什么是VBA,它有什么作用 VBA在哪里存放的?怎么运行的 什么是宏?宏和VBA有什么关系? 录制一个宏 编写第一个宏 一.什么是VBA,它有什么作用 VBA是一种编程语言,它依托于Office ...
- top命令 Linux查看CPU和内存使用情况,cpu监控之一
一.top命令 top命令是一个功能十分强大的监控系统的工具,对于系统管理员而言尤其重要.但是,它的缺点是会消耗很多系统资源. 在系统维护的过程中,随时可能有需要查看 CPU 使用率,并根据相应信息分 ...
- mycat 实践扩容
实践扩容, travelrecord表定义为10个分片,尝试将10个分片中的2个分片转移到第二台MySQL上 1--定义10个分片<table name="travelrecord_t ...
- 用Unreal Engine绘制实时CG影像
转自:http://www.unrealchina.net/portal.php?mod=view&aid=225 近年来,对实时CG的关心热度越来越高,但要想弥补与预渲染方式的差异并不是那么 ...
- mybatis---demo1--(n-n)----bai
实体类1: package com.etc.entity; import java.util.List; public class RoleInfo { private int rid; privat ...
- [转]浅谈javascript函数劫持
转自:Ph4nt0m Security Team 这么多年了,现在学习依然还是有很多收货,向前辈致敬.转载一方面是自己存档一份,另一方面是让更多喜欢安全的人一同学习. ================ ...
- PHP类(三)-类的封装
设置私有成员 使用private关键字来设置私有成员,完成对成员的封装,封装后的成员在对象的外部不能被访问,如果访问会出现错误,在对象的内部能访问被封装的成员属性和方法. <?php class ...
- 第八章 JVM内存管理(待续)
物理内存与虚拟内存 内核空间与用户空间 在Java中哪些组件需要使用内存 JVM内存结构 JVM内存分配策略 JVM内存回收策略 内存问题分析
- 问题:oracle decode;结果:oracle中的decode的使用
oracle中的decode的使用 Oracle 中 decode 函数用法 含义解释:decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下:IF 条件 ...