原题链接在这里:https://leetcode.com/problems/ipo/description/

题目:

Suppose LeetCode will start its IPO soon. In order to sell a good price of its shares to Venture Capital, LeetCode would like to work on some projects to increase its capital before the IPO. Since it has limited resources, it can only finish at most k distinct projects before the IPO. Help LeetCode design the best way to maximize its total capital after finishing at most k distinct projects.

You are given several projects. For each project i, it has a pure profit Pi and a minimum capital of Ci is needed to start the corresponding project. Initially, you have W capital. When you finish a project, you will obtain its pure profit and the profit will be added to your total capital.

To sum up, pick a list of at most k distinct projects from given projects to maximize your final capital, and output your final maximized capital.

Example 1:

Input: k=2, W=0, Profits=[1,2,3], Capital=[0,1,1].

Output: 4

Explanation: Since your initial capital is 0, you can only start the project indexed 0.
After finishing it you will obtain profit 1 and your capital becomes 1.
With capital 1, you can either start the project indexed 1 or the project indexed 2.
Since you can choose at most 2 projects, you need to finish the project indexed 2 to get the maximum capital.
Therefore, output the final maximized capital, which is 0 + 1 + 3 = 4.

Note:

  1. You may assume all numbers in the input are non-negative integers.
  2. The length of Profits array and Capital array will not exceed 50,000.
  3. The answer is guaranteed to fit in a 32-bit signed integer.

题解:

找出每次在现有capital内最大profit的project.

利用两个heap 保存 capital 和 profit的对, 一个 min heap 从小到大按照capital保存. 一个max heap从大到小按照profit保存.

比现有capital小的所有对都从 min heap中poll出来加到max heap里, 顶头的就是小于capital最大profit的project.

Time Complexity: O(k*logn). n = Profits.length.

Space: O(n).

AC Java:

 class Solution {
public int findMaximizedCapital(int k, int W, int[] Profits, int[] Capital) {
PriorityQueue<int []> capMinHeap = new PriorityQueue<int []>((a,b) -> a[0]-b[0]);
PriorityQueue<int []> proMaxHeap = new PriorityQueue<int []>((a,b) -> b[1]-a[1]); for(int i = 0; i<Profits.length; i++){
capMinHeap.add(new int[]{Capital[i], Profits[i]});
} for(int i = 0; i<k; i++){
while(!capMinHeap.isEmpty() && capMinHeap.peek()[0]<=W){
proMaxHeap.add(capMinHeap.poll());
} if(proMaxHeap.isEmpty()){
break;
} W += proMaxHeap.poll()[1];
} return W;
}
}

LeetCode IPO的更多相关文章

  1. [LeetCode] IPO 上市

    Suppose LeetCode will start its IPO soon. In order to sell a good price of its shares to Venture Cap ...

  2. [LeetCode解题报告] 502. IPO

    题目描述 假设 LeetCode 即将开始其 IPO.为了以更高的价格将股票卖给风险投资公司,LeetCode希望在 IPO 之前开展一些项目以增加其资本. 由于资源有限,它只能在 IPO 之前完成最 ...

  3. 【LeetCode】502. IPO

    题目 假设 LeetCode 即将开始其 IPO.为了以更高的价格将股票卖给风险投资公司,LeetCode希望在 IPO 之前开展一些项目以增加其资本. 由于资源有限,它只能在 IPO 之前完成最多 ...

  4. Leetcode 502.IPO

    IPO 假设 LeetCode 即将开始其 IPO.为了以更高的价格将股票卖给风险投资公司,LeetCode希望在 IPO 之前开展一些项目以增加其资本. 由于资源有限,它只能在 IPO 之前完成最多 ...

  5. [LeetCode] 502. IPO 上市

    Suppose LeetCode will start its IPO soon. In order to sell a good price of its shares to Venture Cap ...

  6. Java实现 LeetCode 502 IPO(LeetCode:我疯起来连自己都卖)

    502. IPO 假设 力扣(LeetCode)即将开始其 IPO.为了以更高的价格将股票卖给风险投资公司,力扣 希望在 IPO 之前开展一些项目以增加其资本. 由于资源有限,它只能在 IPO 之前完 ...

  7. 第九周 Leetcode 502. IPO (HARD)

    Leetcode 502 一个公司 目前有资产W 可以选择实现K个项目,每个项目要求公司当前有一定的资产,且每个项目可以使公司的总资产增加一个非负数. 项目数50000 设计一个优先队列,对于当前状态 ...

  8. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  9. [Swift]LeetCode502. IPO(首次公开募股) | Initial Public Offerings

    Suppose LeetCode will start its IPO soon. In order to sell a good price of its shares to Venture Cap ...

随机推荐

  1. Linux内核参数之rp_filter

    一.rp_filter参数介绍 rp_filter参数用于控制系统是否开启对数据包源地址的校验. 首先看一下Linux内核文档documentation/networking/ip-sysctl.tx ...

  2. 20165101刘天野 2018-2019-2《网络对抗技术》Exp4 恶意代码分析

    20165101刘天野 2018-2019-2<网络对抗技术>Exp4 恶意代码分析 1. 实践目标 1.1是监控你自己系统的运行状态,看有没有可疑的程序在运行. 1.2是分析一个恶意软件 ...

  3. CentOS 7 安装各个桌面版本

    http://unix.stackexchange.com/questions/181503/how-to-install-desktop-environments-on-centos-7 92dow ...

  4. cnetos升级内核玩docker

    最近在学习docker容器.在阿里云上的服务器内核版本比较低.所以,需要先升级. 查看内核命令:uname -r 升级内核,网上也有很多种方式.一般都是下载内核包,然后自己编译.不过这种方式需要注意的 ...

  5. C++ 学习使用

    1.C++已经不太建议用#define const用法 定义小函数,不改变内部成员的可以用inline 2.namespace使用 3.C语言预处理 #define这些的使用 #pragma once ...

  6. java多线程学习三

    本章主要学习线程的静态方法 1.先忙先看一段代码: public class MyThread3 implements Runnable { static { System.out.println(& ...

  7. C#—序列化(Serialize)和反序列化(NonSerialize)

    (转自:http://www.cnblogs.com/Abel-Zhang/p/Serialize.html) 一.概述 序列化是把对象转变成流.相反的过程就是反序列化. 哪些场合用到这项技术呢? 1 ...

  8. 【MAF】MAF插件框架简介

    引言    Managed Add-In Framework是一个插件式框架.它有两大作用,一个是解耦,插件和宿主由7个管道组成,两边都有适配器Adapter管道,能最大程度地降低插件和宿主的耦合度: ...

  9. 简单使用JDOM解析XML

    原文:http://liuwentao.iteye.com/blog/59978 使用JDOM解析XML一.前言JDOM是Breet Mclaughlin和Jason Hunter两大Java高手的创 ...

  10. 剑指offer--24.树的子结构

    时间限制:1秒 空间限制:32768K 热度指数:407165 题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构)   class Solution ...