当前位置:网站首页>[leetcode] 502.ipo (difficult)
[leetcode] 502.ipo (difficult)
2022-07-25 22:21:00 【Bright morning light】
One 、 subject
1、 Title Description
hypothesis Power button (LeetCode) begin in a minute IPO . In order to sell shares to venture capital companies at a higher price , Power button Hope that in IPO Some projects have been carried out to increase its capital . Due to limited resources , It can only be found in IPO Most completed before k Different projects . help Power button Design completed at most k How to get the maximum total capital after different projects .
Here you are. n A project . For each project i , It has a net profit profits[i] , And the minimum capital required to start the project capital[i] .
first , Your capital is w . When you finish a project , You will get a net profit , And profits will be added to your total capital .
To make a long story short , Select... From a given project most k List of different items , With Maximize final capital , And export the most capital available .
The answer is guaranteed in 32 Bit signed integer range .
Example 1:
Input :k = 2, w = 0, profits = [1,2,3], capital = [0,1,1]
Output :4
explain :
Because your initial capital is 0, You can only from 0 Project start .
After completion , You will receive 1 The profits of the , Your total capital will become 1.
Now you can choose to start 1 Number or 2 Item No. .
Because you can choose up to two items , So you need to finish 2 Project to maximize capital .
therefore , Export the ultimate maximized capital , by 0 + 1 + 3 = 4.
Example 2:
Input :k = 3, w = 0, profits = [1,2,3], capital = [0,1,2]
Output :6
2、 Basic framework
class Solution {
public:
int findMaximizedCapital(int k, int w, vector<int>& profits, vector<int>& capital) {
}
};
3、 Original link
Two 、 Problem solving report
1、 Thought analysis
(1) Prepare a small root pile , Put all items in this heap , use cost Sort .
(2) Prepare a large root pile , It's empty at first , use profits Sort .
(3) be based on M M M, Pop up the items that can be done in the small root pile x x x, Put it into the big root pile ( Sort by profit ), Pop up the top item of the big root pile , Just do the project , After finishing the project , M = M + p r o f i t s [ x ] M = M+profits[x] M=M+profits[x]. Repeat the process until complete K K K A project .
In conclusion , Namely :
- M M M Go to unlock the items in the small root heap , Put it into the big root pile ;
- Pop up the item at the top of the big root heap , Just do it ;
- Repeat the above two processes , Until it's done K K K A project
2、 Time complexity
O ( n × l o g n ) O(n \times logn) O(n×logn)
3、 Code details
C++ edition :
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;
// All items are added to the minimum cost heap
for (int i = 0; i < profits.size(); i++) {
mincapitalQue.push(new Program(capitals[i], profits[i]));
}
// The projects that can be done are put into the maximum income pile
for (int i = 0; i < k; i++) {
while (!mincapitalQue.empty() && mincapitalQue.top()->capital <= w) {
maxProfitQue.push(mincapitalQue.top());
mincapitalQue.pop();
}
// When used w When you can't do any projects , Go straight back to
if (maxProfitQue.empty()) return w;
w += maxProfitQue.top()->profit;
maxProfitQue.pop();
}
return w;
}
};
Java edition :
public class Solution {
// most K A project
// W It's the initial capital
// Profits[] Capital[] It must be the same length
// Return the final maximum funds
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());
}
// When used W When you can't do any projects , Go straight back .
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;
}
}
}
边栏推荐
- Advanced database · how to add random data for data that are not in all user data - Dragonfly Q system users without avatars how to add avatar data - elegant grass technology KIR
- Leetcode 106. 从中序与后序遍历序列构造二叉树
- Wechat applet (anti shake, throttling), which solves the problem that users keep pulling down refresh requests or clicking buttons to submit information; Get the list information and refresh the data
- The automation testing post spent 20K recruiting, but in the end, there was no suitable one. Both fresh students are better than them
- Jenkins+svn configuration
- Playwright tutorial (II) suitable for Xiaobai
- 数据库进阶·如何针对所有用户数据中没有的数据去加入随机的数据-蜻蜓Q系统用户没有头像如何加入头像数据-优雅草科技kir
- xxl-job中 关于所有日志系统的源码的解读(一行一行源码解读)
- Tfrecord write and read
- 数据质量:数据治理的核心
猜你喜欢

Usage of in in SQL DQL query

After three years of software testing at Tencent, I was ruthlessly dismissed in July, trying to wake up my brother who was paddling

kubernetes之VictoriaMetrics单节点

3. Editors (vim)

『SignalR』. Net using signalr for real-time communication

3dslicer import cone beam CT image

On the difference between break and continue statements

Redis基础2(笔记)

ThreadLocal 总结(未完待续)

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
随机推荐
Xiaobai programmer's seventh day
On the difference between break and continue statements
什么是类加载?类加载的过程?
Xiaobai programmer's first day
3dslicer importing medical image data
[assembly language 01] basic knowledge
Interpretation of the source code of all logging systems in XXL job (line by line source code interpretation)
Visitor mode
三菱FX PLC自由口RS指令实现MODBUS通讯
数学规划分类 Math Programming Classfication
According to the use and configuration of data permissions in the open source framework
vim用法记录
什么是分区分桶?
SQL基本语句 DQL select与提取 DML插入删除
如何实现一个App应用程序,限制用户时间使用?
Gan, why '𠮷 𠮷'.Length== 3 ??
6-18 vulnerability exploitation - backdoor connection
ML-Numpy
C language: random generated number + selective sorting
访问者模式(visitor)模式