[LeetCode] 502. 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 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 题目总结

posted @ 2023-02-24 02:21  CNoodle  阅读(91)  评论(0编辑  收藏  举报