//原题链接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. Spark - spark on yarn 的作业提交流程

    YarnClient YarnCluster 客户端(Client)通过YARN的ResourceManager提交应用程序.在此过程中,客户端进行权限验证,生成Job ID和资源上传路径,并将这些信 ...

  2. AngleSharp :在 C# 中轻松解析和操作 HTML/XML 文档

    AngleSharp 是一个 C# 库,主要用于解析和操作 HTML 和 XML 文档,类似于浏览器的 DOM 操作.允许你在 C# 中使用类似浏览器的方式处理网页数据,进行网页抓取.数据提取和处理等 ...

  3. “未能加载工具箱项xxx,将从工具箱中将其删除”提示出现原因及解决方案

    https://www.thinbug.com/q/27289366 https://social.msdn.microsoft.com/Forums/vstudio/en-US/77e10b58-4 ...

  4. 【Abaqus热分析】热膨胀系数设置

    来源:帮助文档

  5. Open-Sora 2.0 重磅开源!

    潞晨科技正式推出 Open-Sora 2.0 -- 一款全新开源的 SOTA 视频生成模型,仅 20 万美元(224 张 GPU)成功训练商业级 11B 参数视频生成大模型.开发高性能的视频生成模型通 ...

  6. go gin Next()方法

    示例 gin Next()使用方法 package main import ( "fmt" "github.com/gin-gonic/gin" "n ...

  7. go strings包

    //是否包含指定的字符串中任意一个字符 有一个出现过 就返回true fmt.Println(strings.ContainsAny(s1,"glass")) //返回指定字符出现 ...

  8. .NET周刊【3月第2期 2025-03-09】

    国内文章 记一次.NET内存居高不下排查解决与启示 https://www.cnblogs.com/huangsheng/p/18731382 本文讲述了一个ASP.NET Core gRPC服务迁移 ...

  9. CentOS7安装图形界面模式

    0.9272019.03.24 15:17:05字数 865阅读 22,115 本人通过VMware14安装Centos7.6过程中出现蓝屏现象,最后发现是因为在系统安装图形界面过程中报错导致,所以决 ...

  10. javascript for...in

    在JS中我们最常见的循环语句是for循环语句,一个简单的for循环语句如下: for(var i = 0, n = 100; i < n; i++){ // to do somethings . ...