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;
    }
}
相关推荐
AKA__Zas18 分钟前
芝士算法(前缀和2.0)
java·数据结构·算法·leetcode·哈希算法·学习方法
caishenzhibiao25 分钟前
降雨带波段点差 同花顺期货通指标
java·c语言·c#
皓月斯语32 分钟前
B3867 [GESP202309 三级] 小杨的储蓄 题解
c++·算法·题解
七夜zippoe42 分钟前
OpenClaw Prompt 工程:从角色设定到 Skill 封装的 Agent 输出质量方法论
java·服务器·prompt·openclaw·skill封装
rosmis1 小时前
agent各指标定义
android·java·开发语言
mifengxing1 小时前
Java 集合进阶(一)
java·开发语言·数据结构·复习笔记
令狐前生1 小时前
Intellij IDEA 2025 破解安装
java·ide·intellij-idea
Tisfy2 小时前
LeetCode 0486.预测赢家:深度优先搜索(DFS)
算法·leetcode·深度优先·dfs·博弈
数聚天成DeepSData2 小时前
数聚天成 DeepSData 数据价值落地实战指南
java·maven·devops
小小小米粒2 小时前
阿姆达尔定律(Amdahl‘s Law)
java·开发语言