//原题链接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. Docker - 在线音乐播放器 YesPlayMusic

    原文链接:https://mp.weixin.qq.com/s/D2778fnix6jAeZlCicWGdw(本文只作为跟练,不以盈利为目的) 1.下载镜像 [root@node ~]# docker ...

  2. thinkphp6实现仿微信朋友圈,用户可发布图片和文字内容,用户可评论,其他用户可评论文章,也可回复用户评论,多层级评论,无限级评论

    功能:仿微信朋友圈,用户可发布图片和文字内容,用户可评论,其他用户可评论文章,也可回复用户评论,多层级评论,无限级评论数据库示例:朋友圈内容表 article表:id content image li ...

  3. 【MATLAB习题】铰链四杆机构的运动学分析

    铰链四杆机构题目&已知数据 matlab 代码 主程序文件: function main %输入已知数据 clear; i1=101.6; i2=254; i3=177.8; i4=304.8 ...

  4. [tldr] 使用ip.sb检查自己所在局域网的公网IP

    使用ip a等一些命令行工具可以帮助我们检查自己的内网IP,但是,如何获取自己的在公网下的IP(即当前所在的局域网被分配的公网IP) 如果使用爬虫,这个IP也是很重要的.BAN IP就是这个IP ht ...

  5. containerd 配置使用私有镜像仓库 harbor

    前言 ​当要从非安全的镜像仓库中进行 Pull.Push 时,会遇到 x509: certificate signed by unknown authority 错误提示: 这是由于镜像仓库是可能是 ...

  6. MySQL REPLACE INTO语句

    介绍 在向表中插入数据时,我们经常会:首先判断数据是否存在:如果不存在,则插入:如果存在,则更新. 但在 MySQL 中有更简单的方法,replace into(insert into 的增强版),当 ...

  7. MySQL 8.0 误删了root用户怎么办

    MySQL 8.0 误删了root用户怎么办 修改配置文件 修改配置文件,让其可以无账号登录 默认的文件为: /etc/my.cnf 添加:skip-grant-tables 重启服务 service ...

  8. SpringBoot原理分析-1

    SpringBoot原理分析 作为一个javaer,和boot打交道是很常见的吧.熟悉boot的人都会知道,启动一个springboot应用,就是用鼠标点一下启动main方法,然后等着就行了.我们来看 ...

  9. MaxKB+Ollama 离线部署

    主题:在 Centos7 环境部署 MaxKB 以及 Ollama 实现基于离线大模型的的小助手调用. 选择离线部署的原因:原计划是打算直接使用 1Panel 进行 MaxKB 和 Ollama 一键 ...

  10. Codeforces Round 954 (Div. 3)

    A. X Axis 1.既然要求每个点到a到距离之和最小,不妨让点a为3个点中的中间点,也就是先对三个数从小到大排序,然后输出首尾数减中间值的绝对值之和即可 #include <bits/std ...