//原题链接https://leetcode.com/problems/ipo/

  • 题目描述

    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:

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

  • 思路分析
    //IPO首次公开招募
    在有w本金的情况下,完成k个项目,在资本足够的情况下寻找最大利润,在做完k个项目或者资金不足结束。
    直接法:类似于背包,都可以采用贪心思想,选取当前资本足够且获益最高的项目,依次迭代。
  • 源码附录
    class Solution {
    public class Node{
    public int cost;
    public int profit; public Node(int p,int c){
    profit = p;
    cost = c;
    }
    } public int findMaximizedCapital(int k, int W, int[] Profits, int[] Capital) {
    PriorityQueue<Node> minCost = new PriorityQueue<Node>(new Comparator<Node>(){
    public int compare(Node n,Node m){
    return n.cost-m.cost;
    }
    });
    PriorityQueue<Node> maxProfit = new PriorityQueue<Node>(new Comparator<Node>(){
    public int compare(Node n,Node m){
    return m.profit-n.profit;
    }
    }); Node cur;
    for(int i=0;i<Capital.length;i++){
    cur = new Node(Profits[i],Capital[i]);
    minCost.add(cur);
    } for(int i=0;i<k;i++){
    //亲娘咧 一定要记得小堆为空的时候 找了N久
    while (!minCost.isEmpty() && minCost.peek().cost <= W) {
    maxProfit.add(minCost.poll());
    }
    if(maxProfit.isEmpty()){
    return W;
    }
    W = W + maxProfit.poll().profit;
    }
    return W;
    }
    }

IPO——LeetCode⑫的更多相关文章

  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 IPO

    原题链接在这里:https://leetcode.com/problems/ipo/description/ 题目: Suppose LeetCode will start its IPO soon. ...

  5. Leetcode 502.IPO

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

  6. [LeetCode] 502. IPO 上市

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

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

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

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

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

  9. Swift LeetCode 目录 | Catalog

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

  10. [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. 大量小文件不适合存储于HDFS的原因

    1.小文件过多,会过多占用namenode的内存,并浪费block. - 文件的元数据(包括文件被分成了哪些blocks,每个block存储在哪些服务器的哪个block块上),都是存储在namenod ...

  2. 「六」Goaccess实现可视化

    下载 apt install goaccess 使用goaccess进行监控 LANG="en_US.UTF-8" bash -c 'goaccess logs/access.lo ...

  3. RealityCapture重建试验

    一.使用已有数据集 (一)小型物件(官网) 输入:Camera_Lubitel2_studio "Lubitel Camera" consisting of 72 images 地 ...

  4. 【虚拟机】Windows(x86)上部署ARM虚拟机(Ubuntu)

    [虚拟机]Windows(x86)上部署ARM虚拟机(Ubuntu) 零.起因 最近在学嵌入式,这就不得不涉及ARM指令集,但是电脑是x86指令集的,用手机不太方便,买开发板又要等几天--,总之就是要 ...

  5. CoreOS 更新重启后, 所有容器服务全部停掉了

    今天有几个服务出问题了,上去看了下,这台 CoreOS 下的所有容器服务竟然全部停掉了,好奇怪,启动容器时明明加了--detach参数了呀. 问题原因 想了想,会不是是 CoreOS 更新重启导致的, ...

  6. IP地址查询服务

    IP地址查询站点 https://ip.cn/ http://ip.qq.com/ http://ip138.com/ https://www.apnic.net/ ... IP计算 ip地址在线计算 ...

  7. unity里生成的.csproj和.sln :assembly definition以及ET框架中的程序集定义

    有一段时间一直没明白为啥有的时候第三方的package里的代码没法引用我们项目的,最近有点心得,记录下: 在创建unity项目的时候默认是创建一个解决方案就是以.sln为结尾的.默认开发时都在同一个解 ...

  8. Postwoman教程

    1.安装 打开git或cmder,输入如下命令: cd d:/GitDemo/ git clone https://github.com/liyasthomas/postwoman cd postwo ...

  9. 🎀SpringBoot启动创建系统托盘及功能

    简介 SpringBoot启动时,创建系统托盘,提供打开主程序及退出功能. 实现 启动类添加构造函数 public TjtoolApplication() { initUI(); } private ...

  10. python爬虫爬取B站视频字幕,简单的数据处理(pandas将字幕写入到CSV文件中)

    上文,我们爬取到B站视频的字幕:https://www.cnblogs.com/becks/p/14540355.html 这篇,讲讲怎么把爬到的字幕写到CSV文件中,以便用于后面的分析 本文主要用到 ...