LeetCode 502. IPO
原题链接
困难
作者:
toFuture
,
2025-04-02 00:17:47
·江苏
,
所有人可见
,
阅读 1
贪心,每次选利润最大的
class Solution {
public:
int findMaximizedCapital(int k, int w, vector<int>& profits, vector<int>& capital) {
vector<pair<int, int>> q;
int n=profits.size();
for(int i=0;i<n;i++)q.push_back({capital[i], profits[i]});
sort(q.begin(), q.end());
priority_queue<int> heap;
int i=0;
while(k--)
{
while(i<n&&q[i].first<=w)heap.push(q[i++].second);
if(heap.empty())break;
auto t=heap.top();
w+=t;
heap.pop();
}
return w;
}
};