LeetCode 题解工作台

IPO

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

category

4

题型

code_blocks

4

代码语言

hub

3

相关题

当前训练重点

困难 · 贪心·invariant

bolt

答案摘要

将每个项目放入优先队列 中,按照启动资本从小到大排序。如果堆顶元素启动资本不超过当前已有的资金,则循环弹出,放入另一个优先队列 中,按照纯利润从大到小排序。取出当前利润最大的项目,将其纯利润加入到当前资金中,重复上述操作 次。 时间复杂度 $O(n\log n)$,空间复杂度 。其中 为项目数。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 贪心·invariant 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

假设 力扣(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 <= 105
  • 0 <= w <= 109
  • n == profits.length
  • n == capital.length
  • 1 <= n <= 105
  • 0 <= profits[i] <= 104
  • 0 <= capital[i] <= 109
lightbulb

解题思路

方法一:贪心 + 优先队列(双堆)

将每个项目放入优先队列 q1q_1 中,按照启动资本从小到大排序。如果堆顶元素启动资本不超过当前已有的资金,则循环弹出,放入另一个优先队列 q2q_2 中,按照纯利润从大到小排序。取出当前利润最大的项目,将其纯利润加入到当前资金中,重复上述操作 kk 次。

时间复杂度 O(nlogn)O(n\log n),空间复杂度 O(n)O(n)。其中 nn 为项目数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • 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.

warning

常见陷阱

外企场景
  • 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.

swap_horiz

进阶变体

外企场景
  • 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.

help

常见问题

外企场景

IPO题解:贪心·invariant | LeetCode #502 困难