当前位置:网站首页>【Leetcode】502.IPO(困难)
【Leetcode】502.IPO(困难)
2022-07-25 22:20:00 【明朗晨光】
一、题目
1、题目描述
假设 力扣(LeetCode)即将开始 IPO 。为了以更高的价格将股票卖给风险投资公司,力扣 希望在 IPO 之前开展一些项目以增加其资本。 由于资源有限,它只能在 IPO 之前完成最多 k 个不同的项目。帮助 力扣 设计完成最多 k 个不同项目后得到最大总资本的方式。
给你 n 个项目。对于每个项目 i ,它都有一个纯利润 profits[i] ,和启动该项目需要的最小资本 capital[i] 。
最初,你的资本为 w 。当你完成一个项目时,你将获得纯利润,且利润将被添加到你的总资本中。
总而言之,从给定项目中选择 最多k 个不同项目的列表,以 最大化最终资本 ,并输出最终可获得的最多资本。
答案保证在 32 位有符号整数范围内。
示例1:
输入:k = 2, w = 0, profits = [1,2,3], capital = [0,1,1]
输出:4
解释:
由于你的初始资本为 0,你仅可以从 0 号项目开始。
在完成后,你将获得 1 的利润,你的总资本将变为 1。
此时你可以选择开始 1 号或 2 号项目。
由于你最多可以选择两个项目,所以你需要完成 2 号项目以获得最大的资本。
因此,输出最后最大化的资本,为 0 + 1 + 3 = 4。
示例2:
输入:k = 3, w = 0, profits = [1,2,3], capital = [0,1,2]
输出:6
2、基础框架
class Solution {
public:
int findMaximizedCapital(int k, int w, vector<int>& profits, vector<int>& capital) {
}
};
3、原题链接
二、解题报告
1、思路分析
(1)准备一个小根堆,将所有项目放入该堆中,用花费进行排序。
(2)准备一个大根堆,一开始为空,用 利润 进行排序。
(3)基于 M M M,弹出小根堆中能做的项目 x x x,放入大根堆中(按利润排序),弹出大根堆的堆顶项目,就做该项目,做完该项目后, M = M + p r o f i t s [ x ] M = M+profits[x] M=M+profits[x]。重复该过程直到完成 K K K 个项目。
总结来说,就是:
- M M M 去解锁小根堆中的项目,放入大根堆中;
- 弹出大根堆顶部的项目,就做它;
- 重复以上两个过程,直到做完 K K K 个项目
2、时间复杂度
O ( n × l o g n ) O(n \times logn) O(n×logn)
3、代码详解
C++ 版:
class Solution {
public:
class Program {
public:
int capital;
int profit;
Program(int c, int p) : capital(c), profit(p) {
}
};
struct mincapitalCMP {
bool operator()(Program* const &p1, Program* const &p2) const {
return p1->capital > p2->capital;
}
};
struct maxProfitCMP {
bool operator()(Program* const &p1, Program* const &p2) const {
return p1->profit < p2->profit;
}
};
int findMaximizedCapital(int k, int w, vector<int> &profits, vector<int> &capitals) {
priority_queue<Program*, vector<Program*>, mincapitalCMP> mincapitalQue;
priority_queue<Program*, vector<Program*>, maxProfitCMP> maxProfitQue;
//所有项目加入到最小花费的堆中
for (int i = 0; i < profits.size(); i++) {
mincapitalQue.push(new Program(capitals[i], profits[i]));
}
//能做的项目放到最大收益堆中
for (int i = 0; i < k; i++) {
while (!mincapitalQue.empty() && mincapitalQue.top()->capital <= w) {
maxProfitQue.push(mincapitalQue.top());
mincapitalQue.pop();
}
//当用w做不了任何项目的时候,直接返回
if (maxProfitQue.empty()) return w;
w += maxProfitQue.top()->profit;
maxProfitQue.pop();
}
return w;
}
};
Java版:
public class Solution {
// 最多K个项目
// W是初始资金
// Profits[] Capital[] 一定等长
// 返回最终最大的资金
public static int findMaximizedCapital(int K, int W, int[] Profits, int[] Capital) {
PriorityQueue<Program> minCostQ = new PriorityQueue<>(new MinCostComparator());
PriorityQueue<Program> maxProfitQ = new PriorityQueue<>(new MaxProfitComparator());
for (int i = 0; i < Profits.length; i++) {
minCostQ.add(new Program(Profits[i], Capital[i]));
}
for (int i = 0; i < K; i++) {
while (!minCostQ.isEmpty() && minCostQ.peek().c <= W) {
maxProfitQ.add(minCostQ.poll());
}
//当用W做不了任何项目的时候,就直接返回。
if (maxProfitQ.isEmpty()) {
return W;
}
W += maxProfitQ.poll().p;
}
return W;
}
public static class Program {
public int p;
public int c;
public Program(int p, int c) {
this.p = p;
this.c = c;
}
}
public static class MinCostComparator implements Comparator<Program> {
@Override
public int compare(Program o1, Program o2) {
return o1.c - o2.c;
}
}
public static class MaxProfitComparator implements Comparator<Program> {
@Override
public int compare(Program o1, Program o2) {
return o2.p - o1.p;
}
}
}
边栏推荐
- How to implement an app application to limit users' time use?
- [fan Tan] those stories that seem to be thinking of the company but are actually very selfish (I: building wheels)
- Synchronized and volatile
- ThreadLocal 总结(未完待续)
- Wechat applet application development competition works comprehensive development record - Jinlu cultural tourism (cloud development - Overview)
- 『SignalR』. Net using signalr for real-time communication
- Get together for ten years, tell your story, millions of gifts are waiting for you
- 在进行自动化测试,遇到验证码的问题,怎么办?
- [dinner talk] those things that seem to be for the sake of the company but are actually incomprehensible (2: soft quality "eye edge" during interview)
- 完啦,上班三个月,变秃了
猜你喜欢
随机推荐
Fill the whole square with the float property
Arcgis10.2 configuring postgresql9.2 standard tutorial
jenkins+SVN配置
JMeter websocket接口测试
The testing work is not valued. Have you changed your position?
Acwing 866. determining prime numbers by trial division
C language: random generated number + selective sorting
Wet- a good choice for people with English difficulties - console translation
What have I experienced to become a harder tester than development?
QML module not found
Leetcode 106. construct binary tree from middle order and post order traversal sequence
测试工作不受重视,你换位思考了吗?
Common source code for ArcGIS development
Xiaobai programmer's fourth day
vim用法记录
Mitsubishi FX PLC free port RS command realizes Modbus Communication
Some summary about function
Why does redis choose single thread?
Unity performance optimization direction
Imitation Tiktok homepage interface









