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;
    }
}
相关推荐
沐欣工作室_lvyiyi1 小时前
基于SpringBoot的博物馆预约系统设计与实现(论文+源码)
java·spring boot·后端·计算机毕业设计
代码什么用2 小时前
LeetCode-42. 接雨水
算法·leetcode
程序员清风2 小时前
Java 智能体开发:从对话接口到任务执行
java·人工智能·python
一 乐2 小时前
博客管理系统|基于springboot + vue博客管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·毕设
QuZhengRong2 小时前
【Spring】后端接收的请求参数多了一个逗号的处理办法
java·后端·spring
ZealSinger2 小时前
Go1.25容器感知GOMAXPROCS避限流
java·开发语言
cxoptics3 小时前
LBO 会不会潮解?存储、镀膜、日常使用注意事项?
java
SL_staff3 小时前
非研发人员搭业务逻辑?先认清这3个可视化编程分水岭
java
王闯写bug3 小时前
tomcat老版本下载tomcat8.5下载
java·tomcat