Leetcode502. IPO
思路:贪心
按照资本第一关键词,利润第二关键字升序排序。总共选择k次,每一次在可选范围之内选择利润最大值
class Solution {
public:
int findMaximizedCapital(int k, int W, vector<int>& profits, vector<int>& capital) {
int n = profits.size();
vector<pair<int, int>> q;
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);
i ++ ;
}
if (!heap.size()) break;
auto t = heap.top();
W += t;
heap.pop();
}
return W;
}
};
LeetCode解题:贪心策略解决IPO问题
1726

被折叠的 条评论
为什么被折叠?



