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;
    }
}
相关推荐
Zhu_S W几秒前
EasyExcel动态表头详解
java·linux·windows
敲代码的哈吉蜂2 分钟前
Nginx配置文件的管理及优化参数
java·服务器·nginx
XiaoLeisj3 分钟前
Android RecyclerView 实战:从基础列表到多类型 Item、分割线与状态复用问题
android·java
勇往直前plus2 小时前
从文件到屏幕:Python/java 字符编码、解码、文本处理的底层逻辑解析
java·开发语言·python
Drifter_yh8 小时前
【黑马点评】Redisson 分布式锁核心原理剖析
java·数据库·redis·分布式·spring·缓存
莫寒清10 小时前
Spring MVC:@RequestParam 注解详解
java·spring·mvc
没有医保李先生10 小时前
字节对齐的总结
java·开发语言
甲枫叶12 小时前
【claude】Claude Code正式引入Git Worktree原生支持:Agent全面实现并行独立工作
java·人工智能·git·python·ai编程
六件套是我12 小时前
无法访问org.springframeword.beans.factory.annotation.Value
java·开发语言·spring boot
LYS_061812 小时前
C++学习(5)(函数 指针 引用)
java·c++·算法