Java | Leetcode Java题解之第502题IPO

题目:

题解:

java 复制代码
class Solution {
    public int findMaximizedCapital(int k, int w, int[] profits, int[] capital) {
        int n = profits.length;
        int curr = 0;
        int[][] arr = new int[n][2];

        for (int i = 0; i < n; ++i) {
            arr[i][0] = capital[i];
            arr[i][1] = profits[i];
        }
        Arrays.sort(arr, (a, b) -> a[0] - b[0]);

        PriorityQueue<Integer> pq = new PriorityQueue<>((x, y) -> y - x);
        for (int i = 0; i < k; ++i) {
            while (curr < n && arr[curr][0] <= w) {
                pq.add(arr[curr][1]);
                curr++;
            }
            if (!pq.isEmpty()) {
                w += pq.poll();
            } else {
                break;
            }
        }

        return w;
    }
}
相关推荐
张文是假的啊13 分钟前
Java | record | Controller逻辑
java·开发语言
勿忘,瞬间19 分钟前
Mybatis高阶
java·数据库·mybatis
Nil2082 小时前
leetcode 994腐烂的橘子
算法·leetcode·职场和发展
嘻哈baby2 小时前
Go 函数中的参数为什么不支持默认值?
java·开发语言·jvm
慧都小项2 小时前
MyEclipse 2026:当 Agent、MCP 与 Java 26 进入 Eclipse 工作流
java·eclipse·springboot·copilot·myeclipse
CoderYanger2 小时前
前端基础——JavaScript(基础语法)(下篇)
java·开发语言·前端·javascript·程序人生·面试·职场和发展
京师20万禁军教头2 小时前
39面向对象(高级)-设计模式
java·开发语言·设计模式
白山编程大哥3 小时前
Java 迭代器接口详解:从 Iterator 到 ListIterator 的完整指南
java·开发语言·windows
热心网友俣先生3 小时前
2026国赛C题:五年命题规律与预测
java·c语言·前端·数学建模
见叶之秋3 小时前
【C++】String类的使用(含模拟实现)
java·开发语言·c++