当前位置:网站首页>【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;
}
}
}
边栏推荐
- After 2 years of functional testing, I feel like I can't do anything. Where should I go in 2022?
- JS interview questions
- SQL basic statement DQL select and extract DML insert delete
- Victoriametrics single node of kubernetes
- El expression improves JSP
- [fan Tan] after the arrival of Web3.0, where should testers go? (ten predictions and suggestions)
- 启牛商学院和微淼商学院哪个靠谱?老师推荐的开户安全吗?
- Internship: writing common tool classes
- What is partition and barrel division?
- The testing work is not valued. Have you changed your position?
猜你喜欢

Win10 set up a flutter environment to step on the pit diary

ArcGIS10.2配置PostgreSQL9.2标准教程

After 2 years of functional testing, I feel like I can't do anything. Where should I go in 2022?

How to resolve a domain name to multiple IP addresses?

Arcgis10.2 configuring postgresql9.2 standard tutorial

SQL中in的用法 DQL 查询

Playwright tutorial (II) suitable for Xiaobai

Tfrecord write and read

MySQL - subquery - column subquery (multi row subquery)

TFrecord写入与读取
随机推荐
3dslicer importing medical image data
Ts:typera code fragment indentation display exception (resolved) -2022.7.24
Playwright tutorial (II) suitable for Xiaobai
Some summary about function
聚名十年,说出你的故事,百万豪礼等你拿
Wechat official account application development (I)
Call of addition, subtraction, multiplication and division of integer type only
自动化测试岗花20K招人,到最后居然没一个合适的,招两个应届生都比他们强吧
After three years of software testing at Tencent, I was ruthlessly dismissed in July, trying to wake up my brother who was paddling
Synchronized and volatile
[assembly language 01] basic knowledge
internship:普通常用的工具类编写
The dragon lizard exhibition area plays a new trick this time. Let's see whose DNA moved?
MySQL --- 子查询 - 列子查询(多行子查询)
What is partition and barrel division?
Having met a tester with three years' experience in Tencent, I saw the real test ceiling
Visitor mode
Why does redis choose single thread?
Randomly generate 10 (range 1~100) integers, save them to the array, and print the array in reverse order. And find the average value, the maximum value and the subscript of the maximum value, and fin
The second short contact of gamecloud 1608