Leetcode_502_IPO_贪心

本文介绍了一种名为Solution的类,用于在给定资本限制下,通过优先级队列算法找到投资组合的最大收益。它首先对项目按资本消耗进行排序,然后动态调整资本分配策略,确保在满足k个项目的前提下最大化剩余资本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值