class Solution {
public int findMaximizedCapital(int k, int w, int[] profits, int[] capital) {
int len = profits.length;
int now = w;
int cnt = k;
int[][] projects = new int[len][2];
for (int i = 0; i < len; i++) {
projects[i][0] = profits[i];
projects[i][1] = capital[i];
}
Arrays.sort(projects, Comparator.comparingInt(o -> o[1]));
PriorityQueue<Integer> pq = new PriorityQueue<>((o1, o2) -> o2 - o1);
for (int[] project : projects) {
while (project[1] > now) {
if (cnt == 0 || pq.isEmpty()) {
return now;
}
cnt--;
now += pq.poll();
}
pq.offer(project[0]);
}
for (int i = 0; i < cnt && !pq.isEmpty(); i++) {
now += pq.poll();
}
return now;
}
}
Leetcode_502_IPO_贪心
最新推荐文章于 2025-07-22 23:28:02 发布
本文介绍了一种名为Solution的类,用于在给定资本限制下,通过优先级队列算法找到投资组合的最大收益。它首先对项目按资本消耗进行排序,然后动态调整资本分配策略,确保在满足k个项目的前提下最大化剩余资本。

341

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



