跳至主要內容

502. IPO


502. IPOopen in new window

🔴   🔖  贪心 数组 排序 堆(优先队列)  🔗 力扣open in new window LeetCodeopen in new window

题目

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 <= 10^5
  • 0 <= w <= 10^9
  • n == profits.length
  • n == capital.length
  • 1 <= n <= 10^5
  • 0 <= profits[i] <= 10^4
  • 0 <= capital[i] <= 10^9

题目大意

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

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

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

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

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

解题思路

可以通过优先队列(堆)来解决。我们可以维护一个大顶堆。每次将总资本允许的条件下所有项目的利润加入堆中,并弹出堆顶元素,将其加入到总利润中,直到做完了 k 个不同项目,最终返回总利润。

  1. 将项目的成本和利润成对加入到数组 vp 中,并按照成本升序排序;
  2. 构建一个大顶堆 heap ,堆中的每个元素是项目的利润;
  3. 重复以下步骤 k 次:
    • 将所有成本 capital 小于总资本 totalProfit 的项目加入堆中;
    • 弹出堆顶元素,并将更新加入总资本 totalProfit 中;
  4. 返回总资本 totalProfit

代码

/**
 * @param {number} k
 * @param {number} w
 * @param {number[]} profits
 * @param {number[]} capital
 * @return {number}
 */
var findMaximizedCapital = function (k, w, profits, capital) {
	let heap = [];
	const insert = (value) => {
		heap.push(value);
		heapifyUp(heap.length - 1);
	};

	const pop = () => {
		if (heap.length == 0) return null;
		const max = heap[0],
			min = heap.pop();
		if (heap.length > 0) {
			heap[0] = min;
			heapifyDown(0);
		}
		return max;
	};

	const heapifyUp = (i) => {
		while (i) {
			const parent = ((i - 1) / 2) | 0;
			if (heap[i] > heap[parent]) {
				[heap[i], heap[parent]] = [heap[parent], heap[i]];
				i = parent;
			} else {
				break;
			}
		}
	};

	const heapifyDown = (i) => {
		let left = i * 2 + 1,
			right = i * 2 + 2,
			min = i;
		if (left < heap.length && heap[left] > heap[min]) {
			min = left;
		}
		if (right < heap.length && heap[right] > heap[min]) {
			min = right;
		}
		if (min !== i) {
			[heap[min], heap[i]] = [heap[i], heap[min]];
			heapifyDown(min);
		}
	};

	let vp = [],
		len = profits.length;

	for (let i = 0; i < len; i++) {
		vp.push([capital[i], profits[i]]);
	}
	vp.sort((a, b) => a[0] - b[0]);

	let totalProfit = w,
		count = 0;
	for (let i = 0; i < k; i++) {
		while (count < len && vp[count][0] <= totalProfit) {
			insert(vp[count][1]);
			count++;
		}
		if (heap.length == 0) {
			break;
		}
		totalProfit += pop();
	}
	return totalProfit;
};

相关题目

题号标题题解标签难度
2542最大子序列的分数open in new window贪心 数组 排序 1+
2813子序列最大优雅度open in new window 贪心 数组 3+