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;
    }
}
相关推荐
avi91116 分钟前
【AI教人】在Inspector显示所有Delegate-Odin Inspector之三
java·前端·算法·delegate·inspector·editor扩展·unity editor
青春不朽51231 分钟前
Python 的 list append 为什么是均摊 O(1)?拆开过度分配那张表,看容量翻倍背后的暗账
java·python·list
小羊没烦恼!1 小时前
.NET 11 Runtime Async 详解
java·运维·开发语言·javascript·python
哇嘎呀1 小时前
RHCSA题目
java·服务器·前端
SL_staff1 小时前
企业知识平台细粒度权限设计实践:从成员组、文档库到操作级的三层防线
java·开源·全栈
周厉2 小时前
ERP系统全景解析:核心模块与业务逻辑框架
java·服务器·前端
傅科摆 _ py2 小时前
链表力扣复习
数据结构·leetcode·链表
开开心心就好2 小时前
课堂随机点名工具,绿色版免安装双击即用
java·服务器·前端·ffmpeg·音视频·perl·symfony
code2cat2 小时前
Java高级篇之Socket:从建立连接到完整收发一条消息
java·开发语言
PD我是你的真爱粉2 小时前
成熟Agent系统设计深度研究报告
java·大数据·人工智能