LeetCode 题解工作台
IPO
假设 力扣(LeetCode)即将开始 IPO 。为了以更高的价格将股票卖给风险投资公司,力扣 希望在 IPO 之前开展一些项目以增加其资本。 由于资源有限,它只能在 IPO 之前完成最多 k 个不同的项目。帮助 力扣 设计完成最多 k 个不同项目后得到最大总资本的方式。 给你 n 个项目。对于每个…
4
题型
4
代码语言
3
相关题
当前训练重点
困难 · 贪心·invariant
答案摘要
将每个项目放入优先队列 中,按照启动资本从小到大排序。如果堆顶元素启动资本不超过当前已有的资金,则循环弹出,放入另一个优先队列 中,按照纯利润从大到小排序。取出当前利润最大的项目,将其纯利润加入到当前资金中,重复上述操作 次。 时间复杂度 $O(n\log n)$,空间复杂度 。其中 为项目数。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 贪心·invariant 题型思路
题目描述
假设 力扣(LeetCode)即将开始 IPO 。为了以更高的价格将股票卖给风险投资公司,力扣 希望在 IPO 之前开展一些项目以增加其资本。 由于资源有限,它只能在 IPO 之前完成最多 k 个不同的项目。帮助 力扣 设计完成最多 k 个不同项目后得到最大总资本的方式。
给你 n 个项目。对于每个项目 i ,它都有一个纯利润 profits[i] ,和启动该项目需要的最小资本 capital[i] 。
最初,你的资本为 w 。当你完成一个项目时,你将获得纯利润,且利润将被添加到你的总资本中。
总而言之,从给定项目中选择 最多 k 个不同项目的列表,以 最大化最终资本 ,并输出最终可获得的最多资本。
答案保证在 32 位有符号整数范围内。
示例 1:
输入:k = 2, w = 0, profits = [1,2,3], capital = [0,1,1] 输出:4 解释: 由于你的初始资本为 0,你仅可以从 0 号项目开始。 在完成后,你将获得 1 的利润,你的总资本将变为 1。 此时你可以选择开始 1 号或 2 号项目。 由于你最多可以选择两个项目,所以你需要完成 2 号项目以获得最大的资本。 因此,输出最后最大化的资本,为 0 + 1 + 3 = 4。
示例 2:
输入:k = 3, w = 0, profits = [1,2,3], capital = [0,1,2] 输出:6
提示:
1 <= k <= 1050 <= w <= 109n == profits.lengthn == capital.length1 <= n <= 1050 <= profits[i] <= 1040 <= capital[i] <= 109
解题思路
方法一:贪心 + 优先队列(双堆)
将每个项目放入优先队列 中,按照启动资本从小到大排序。如果堆顶元素启动资本不超过当前已有的资金,则循环弹出,放入另一个优先队列 中,按照纯利润从大到小排序。取出当前利润最大的项目,将其纯利润加入到当前资金中,重复上述操作 次。
时间复杂度 ,空间复杂度 。其中 为项目数。
class Solution:
def findMaximizedCapital(
self, k: int, w: int, profits: List[int], capital: List[int]
) -> int:
h1 = [(c, p) for c, p in zip(capital, profits)]
heapify(h1)
h2 = []
while k:
while h1 and h1[0][0] <= w:
heappush(h2, -heappop(h1)[1])
if not h2:
break
w -= heappop(h2)
k -= 1
return w
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Candidates who effectively utilize a greedy approach and justify the decision-making process will demonstrate an understanding of the core pattern of the problem.
- question_mark
Look for candidates who efficiently use a heap to manage project selection, as this is key to solving the problem within time limits.
- question_mark
Successful candidates will maintain a clear focus on invariant validation, ensuring that they only choose projects that can be completed with the available capital.
常见陷阱
外企场景- error
Failing to prioritize the most profitable projects or using an inefficient strategy can lead to suboptimal capital maximization.
- error
Incorrectly managing the heap can cause errors in project selection, leading to incorrect answers or inefficiency.
- error
Not correctly handling the situation where projects cannot be completed due to capital constraints, which might result in unnecessary calculations.
进阶变体
外企场景- arrow_right_alt
Consider scenarios where capital is very large or very small compared to the project costs.
- arrow_right_alt
Modify the problem to allow for additional constraints, such as limiting the number of projects from specific categories.
- arrow_right_alt
Change the project selection criteria to focus on minimizing risk or maximizing return over time instead of simply maximizing total capital.