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;
    }
}
相关推荐
葡萄成熟时 !2 小时前
JAVA 常用API学习笔记
java·笔记·学习
鹿角片ljp2 小时前
LeetCode 46. 全排列|吃透回溯
算法·leetcode·职场和发展
Rain的Java大神之路3 小时前
介绍一下分布式事务
java·分布式·后端·spring·spring cloud·架构·springcloud
吴声子夜歌3 小时前
Java面试题——基础(一)
java·开发语言
南城以南溫暖如初1474 小时前
外卖CPS实战指南:从技术选型到运营落地全流程解析
java·spring boot·mysql·架构·vue
傻啦嘿哟4 小时前
英雄联盟皮肤爬虫:爬取全皮肤价格与特效,做比价工具
java·c++·爬虫
.道阻且长.5 小时前
11.LeetCode算法习题讲解--滑动窗口--将x减到0的最小操作数
算法·leetcode·职场和发展
码匠许师傅5 小时前
【C++ 面试真题】24. 聊聊 C++ 的时间日期处理
java·c++·面试
wenyq75 小时前
LeetCode 2460. Apply Operations to an Array
算法·leetcode
mqiqe6 小时前
AgentScope Java 2.0 Agent 状态存储(AgentStateStore)深度解析:构建可恢复、可扩展的智能体运行时
java·运维·网络