首页 > 其他分享 >[LeetCode] 502. IPO

[LeetCode] 502. IPO

时间:2023-02-24 02:33:06浏览次数:49  
标签:int IPO project capital 502 LeetCode profits

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 n projects where the ith project has a pure profit profits[i] and a minimum capital of capital[i] is needed to start it.

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.

Pick a list of at most k distinct projects from given projects to maximize your final capital, and return the final maximized capital.

The answer is guaranteed to fit in a 32-bit signed integer.

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.

Example 2:

Input: k = 3, w = 0, profits = [1,2,3], capital = [0,1,2]
Output: 6

Constraints:

  • 1 <= k <= 105
  • 0 <= w <= 109
  • n == profits.length
  • n == capital.length
  • 1 <= n <= 105
  • 0 <= profits[i] <= 104
  • 0 <= capital[i] <= 109

IPO。

假设 力扣(LeetCode)即将开始 IPO 。为了以更高的价格将股票卖给风险投资公司,力扣 希望在 IPO 之前开展一些项目以增加其资本。 由于资源有限,它只能在 IPO 之前完成最多 k 个不同的项目。帮助 力扣 设计完成最多 k 个不同项目后得到最大总资本的方式。

给你 n 个项目。对于每个项目 i ,它都有一个纯利润 profits[i] ,和启动该项目需要的最小资本 capital[i] 。

最初,你的资本为 w 。当你完成一个项目时,你将获得纯利润,且利润将被添加到你的总资本中。

总而言之,从给定项目中选择 最多 k 个不同项目的列表,以 最大化最终资本 ,并输出最终可获得的最多资本。

答案保证在 32 位有符号整数范围内。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/ipo
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路是贪心。这里我们是要在最初资本 w 允许的情况下,尽可能地先去做启动资金小的项目。在启动资金小的项目中,我们也优先做利润大的。具体实现可以参见代码。

时间O(nlogn)

空间O(n)

Java实现

 1 class Solution {
 2     public int findMaximizedCapital(int k, int w, int[] profits, int[] capital) {
 3         int n = profits.length;
 4         List<int[]> list = new ArrayList<>();
 5         // {capital, profit}
 6         for (int i = 0; i < n; i++) {
 7             list.add(new int[] { capital[i], profits[i] });
 8         }
 9         // 按project的花费从小到大排序
10         Collections.sort(list, (a, b) -> a[0] - b[0]);
11         // 最大堆,利润大的在堆顶
12         PriorityQueue<Integer> queue = new PriorityQueue<>((a, b) -> b - a);
13         int i = 0;
14         while (k-- > 0) {
15             // k和w都允许的情况下尽量放更多的project
16             while (i < n && list.get(i)[0] <= w) {
17                 queue.offer(list.get(i)[1]);
18                 i++;
19             }
20             if (queue.isEmpty()) {
21                 break;
22             }
23             // 累加利润
24             w += queue.poll();
25         }
26         return w;
27     }
28 }

 

LeetCode 题目总结

标签:int,IPO,project,capital,502,LeetCode,profits
From: https://www.cnblogs.com/cnoodle/p/17150017.html

相关文章

  • [LeetCode] 2357. Make Array Zero by Subtracting Equal Amounts
    Youaregivenanon-negativeintegerarray nums.Inoneoperation,youmust:Chooseapositiveinteger x suchthat x islessthanorequaltothe smalle......
  • Leetcode 2569 Handling Sum Queries After Update
    2569. HandlingSumQueriesAfterUpdatYouaregiventwo 0-indexed arrays nums1 and nums2 anda2Darray queries ofqueries.Therearethr......
  • LeetCode56. 合并区间(/)
    原题解题目约束题解classSolution{public:vector<vector<int>>merge(vector<vector<int>>&intervals){if(intervals.size()==0){......
  • LeetCode 55. 跳跃游戏(/贪心)
    原题解题目约束题解classSolution{public:boolcanJump(vector<int>&nums){intn=nums.size();intrightmost=0;for......
  • LeetCode-14. 最长公共前缀(java)
    一、前言:......
  • [Leetcode Weekly Contest]333
    链接:LeetCode[Leetcode]2570.合并两个二维数组-求和法给你两个二维整数数组nums1和nums2.nums1[i]=[idi,vali]表示编号为idi的数字对应的值等于vali。......
  • 【LeetCode】1238. 循环码排列
    【LeetCode】1238.循环码排列题目链接格雷码(循环码)格雷码是一种二进制编码,两个相邻数字的格雷码只有一位二进制位的数码不同。自然码转格雷码数的自然码右移一位和......
  • [Leetcode Weekly Contest]332
    链接:LeetCode[Leetcode]2562.找出数组的串联值给你一个下标从0开始的整数数组 nums。现定义两个数字的串联 是由这两个数值串联起来形成的新数字。例如,15 和 ......
  • 【LeetCode二叉树#04】判断对称二叉树
    对称二叉树力扣题目链接(opensnewwindow)给定一个二叉树,检查它是否是镜像对称的。思路本题中,不能单纯去比较左右子节点的是否对称(都有值且不为空)因为如果按上面那......
  • #yyds干货盘点# LeetCode面试题: 括号生成
    1.简述:数字n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且有效的括号组合。 示例1:输入:n=3输出:["((()))","(()())","(())()","()(())","()()()"]......