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;
    }
}
相关推荐
谷粒.35 分钟前
Cypress vs Playwright vs Selenium:现代Web自动化测试框架深度评测
java·前端·网络·人工智能·python·selenium·测试工具
uzong4 小时前
程序员从大厂回重庆工作一年
java·后端·面试
kyle~4 小时前
C++---value_type 解决泛型编程中的类型信息获取问题
java·开发语言·c++
开心香辣派小星8 小时前
23种设计模式-15解释器模式
java·设计模式·解释器模式
Halo_tjn8 小时前
虚拟机相关实验概述
java·开发语言·windows·计算机
摆烂z9 小时前
Docker与Jib(maven插件版)实战
java
RainbowSea9 小时前
从 Spring Boot 2.x 到 3.5.x + JDK21:一次完整的生产环境迁移实战
java·spring boot·后端
笨手笨脚の9 小时前
Spring Core常见错误及解决方案
java·后端·spring
奶油松果9 小时前
Springboot自动装配 - redis和redission
java·spring boot·redis
霍夫曼9 小时前
UTC时间与本地时间转换问题
java·linux·服务器·前端·javascript