Re: [闲聊] 每日leetcode

楼主: oin1104 (是oin的说)   2024-06-15 12:12:14
题目 :
You are given n projects where the ith project has a pure profit profits[i] and
a minimum capital of capital[i] is needed to start it.
Initially, you have w capital. When you finish a project, you will obtain its pu
re profit and the profit will be added to your total capital.
Pick a list of at most k distinct projects from given projects to maximize your
final capital, and return the final maximized capital.
叫你在k个东西以内赚钱
给你初始钱钱w 跟一堆项目
项目都有最低成本跟获利
问你最多能赚多少钱
思路 :
因为每次都要最大或最小什么的
所以用成priority queue就很好抓要的资料
首先 每次更新完w之后可以做的项目
都丢进去可以做的项目
然后从可以做的项目拿一个最多钱的
做k次
然后就姆咪
```cpp
class Solution {
public:
int findMaximizedCapital(int k, int w, vector<int>& profits, vector<int>& ca
pital)
{
int n = profits.size();
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,in
t>> > cappro;
priority_queue<int , vector<int> , less<int> > pro;
int wnow = w;
for(int i = 0 ; i < n ; i ++)
{
cappro.push({capital[i] , profits[i]});
}
for(int i = 0 ; i < k ; i ++)
{
while(!cappro.empty() && cappro.top().first <= wnow)
{
//cout << cappro.top().first << " " << cappro.top().second << en
dl;
pro.push(cappro.top().second);
cappro.pop();
}
if(pro.empty())break;
wnow += pro.top();
pro.pop();
}
return wnow;
}
};
```

Links booklink

Contact Us: admin [ a t ] ucptt.com