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;
    }
}
相关推荐
a努力。9 分钟前
国家电网Java面试被问:Spring Boot Starter 制作原理
java·spring boot·面试
一 乐9 分钟前
酒店预约|基于springboot + vue酒店预约系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端
LYFlied44 分钟前
【每日算法】LeetCode 136. 只出现一次的数字
前端·算法·leetcode·面试·职场和发展
guslegend1 小时前
Tomact高级使用及原理剖析
java
Code blocks1 小时前
SpringBoot从0-1集成Minio对象存储
java·spring boot·后端
故渊ZY1 小时前
MyBatis事务原理与实战指南
java·mybatis
唯唯qwe-1 小时前
Day23:动态规划 | 爬楼梯,不同路径,拆分
算法·leetcode·动态规划
HTouying1 小时前
线程池【工具类】
java
深盾科技2 小时前
融合C++与Python:兼顾开发效率与运行性能
java·c++·python
我待_JAVA_如初恋2 小时前
idea创建MavenJavaWeb项目以后,包结构缺java
java·ide·intellij-idea