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;
    }
}
相关推荐
落魄大学生之流水线上谋生计14 小时前
LangGraph 基本用法指南
java·windows·python·langchain
2601_9621284114 小时前
SpringBoot篇(缓存层)
java·spring boot·缓存
Dovis(誓平步青云)14 小时前
折叠屏悬停看视频,上半屏和下半屏应该各做什么
android·java·服务器·开发语言·安全·音视频
2501_9378609414 小时前
Java多线程初阶(下)—— synchronized、volatile、wait/notify与经典并发工具
java·开发语言·jvm
牛油果子哥q15 小时前
C++内存池与对象池精讲:内存碎片、自定义分配器、对象池实现、STL allocator原理、工程落地与性能对比
java·开发语言·c++
CodeStats15 小时前
《源纹天书》第三百六十三章至第三百六十四章
java·开发语言·源纹天书
万法若空15 小时前
C/C++ 类型别名定义方式对比
java·c语言·c++
吴声子夜歌15 小时前
Java——开发中通用的方法和准则(一)
java·开发语言
mmsy102415 小时前
缓存知识点总结
java·spring·缓存
运行时异常15 小时前
【WMS 仓储系统集成 AI Agent 实战】第 3 讲:Spring Security 6 + JWT——addFilterBefore 一词之差,全站 401
java·后端