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;
    }
}
相关推荐
Nil20830 分钟前
leetcode 78子集
数据结构·算法·leetcode
用户0942485680335 分钟前
第3章:OpenJDK从源码到字节码——Class 文件与 javac 管线
java·jvm
azhou的代码园43 分钟前
基于RFM模型的中小型企业客户管理系统
java·人工智能·spring boot·毕业设计
君顾11 小时前
酒吧点餐小程序系统开发实战:从架构设计到上线全流程指南
java·开发语言·酒吧
子非鱼a1 小时前
【WEB】[SWPU2019]Web1
java·服务器·前端
花生了什么事o1 小时前
自定义Spring Boot Starter全流程
java·spring boot·后端
torpidcat2 小时前
ruoyi-vue-pro 若依芋道 java springboot +mybatis 生日查询相关
java·vue.js·spring boot
AC赳赳老秦3 小时前
文旅市场公开数据分析:基于 OpenClaw 采集景区客流与门票公示数据,生成区域文旅热度监测报告
java·c语言·python·php·symfony·deepseek·openclaw
我命由我123453 小时前
Windows 操作系统 - 启用 D 盘虚拟内存
java·运维·服务器·windows·学习·java-ee·运维开发
xcl09254 小时前
智慧场馆解决方案系统源码:从架构设计到部署实战
java·spring boot