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;
    }
}
相关推荐
阿米亚波1 分钟前
【C++ STL】std::array
java·开发语言·c++
奶糖 肥晨9 分钟前
mac系统中Java项目环境配置与问题解决全记录| 项目构建篇:Maven编译与打包
java·macos·maven
万亿少女的梦16833 分钟前
基于Spring Boot的乐助在线助农系统设计与实现
java·spring boot·mysql·敏感词过滤·助农系统
唐青枫1 小时前
Java Undertow 实战指南:从轻量 Web Server 到 Spring Boot 嵌入式容器
java
前端工作日常11 小时前
我学习到的Java中domain和dto区别
java·后端
码农学院11 小时前
基于Spring Boot的跨境电商多语言订单管理系统架构设计
java·大数据·spring boot
闪电悠米12 小时前
力扣hot100-48.旋转图像-转置翻转详解
算法·leetcode·职场和发展
技术小结-李爽12 小时前
【工具】pom文件的<packaging>
java·maven
l1564694812 小时前
突围!图文混合知识库难以解析,Kimi-K3 原生视觉架构深耕知识工作,DMXAPI 统一接口,缩短项目开发周期
java·大数据·开发语言
啦啦啦啦啦zzzz13 小时前
算法:贪心算法
c++·算法·leetcode·贪心算法