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;
    }
}
相关推荐
代码or搬砖32 分钟前
公共字段抽取自动填充
android·java·数据库
老华带你飞36 分钟前
校园快递|基于Java校园快递管理系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot
AndreasEmil2 小时前
JavaSE - 继承
java·开发语言·ide·vscode·intellij-idea·idea
后端小张2 小时前
【JAVA 进阶】SpringBoot自动配置机制:从原理到实践的深度解析
java·spring boot·后端·spring·spring cloud·自动配置·注解
毕设源码-赖学姐8 小时前
【开题答辩全过程】以 高校评教评学系统的设计与实现为例,包含答辩的问题和答案
java·eclipse
老华带你飞8 小时前
博物馆展览门户|基于Java博物馆展览门户系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot·后端
路边草随风8 小时前
iceberg 基于 cosn 构建 catalog
java·大数据
It's now8 小时前
Spring Framework 7.0 原生弹性功能系统讲解
java·后端·spring
点PY8 小时前
C++ 中 std::async 和 std::future 的并发性
java·开发语言·c++
一 乐8 小时前
人事管理系统|基于Springboot+vue的企业人力资源管理系统设计与实现(源码+数据库+文档)
java·前端·javascript·数据库·vue.js·spring boot·后端