502:IPO
This commit is contained in:
parent
de21dcd4d2
commit
a22ae134a8
111
src/main/java/leetcode/editor/cn/Ipo.java
Normal file
111
src/main/java/leetcode/editor/cn/Ipo.java
Normal file
@ -0,0 +1,111 @@
|
||||
//假设 力扣(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 <= 10⁵
|
||||
// 0 <= w <= 10⁹
|
||||
// n == profits.length
|
||||
// n == capital.length
|
||||
// 1 <= n <= 10⁵
|
||||
// 0 <= profits[i] <= 10⁴
|
||||
// 0 <= capital[i] <= 10⁹
|
||||
//
|
||||
// Related Topics 贪心 数组 排序 堆(优先队列) 👍 145 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import javafx.util.Pair;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
//502:IPO
|
||||
class Ipo {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new Ipo().new Solution();
|
||||
System.out.println(solution.findMaximizedCapital(2, 0, new int[]{1, 2, 3}, new int[]{0, 1, 1}));
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int findMaximizedCapital(int k, int w, int[] profits, int[] capital) {
|
||||
List<Pair<Integer, Integer>> list = new ArrayList<>();
|
||||
for (int i = 0; i < profits.length; ++i) {
|
||||
list.add(new Pair<>(capital[i], profits[i]));
|
||||
}
|
||||
list.sort(Comparator.comparingInt(Pair::getKey));
|
||||
PriorityQueue<Integer> queue = new PriorityQueue<>((x, y) -> y - x);
|
||||
int index = 0;
|
||||
for (int i = 0; i < k; i++) {
|
||||
while (index < list.size() && list.get(index).getKey() <= w) {
|
||||
queue.add(list.get(index).getValue());
|
||||
index++;
|
||||
}
|
||||
if (!queue.isEmpty()) {
|
||||
w += queue.poll();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return w;
|
||||
}
|
||||
// public int findMaximizedCapital(int k, int w, int[] profits, int[] capital) {
|
||||
// int[][] arr = new int[capital.length][2];
|
||||
// for (int i = 0; i < profits.length; ++i) {
|
||||
// arr[i] = new int[]{capital[i], profits[i]};
|
||||
// }
|
||||
// Arrays.sort(arr, Comparator.comparingInt(o -> o[0]));
|
||||
// PriorityQueue<Integer> queue = new PriorityQueue<>((x, y) -> y - x);
|
||||
// int index = 0;
|
||||
// for (int i = 0; i < k; i++) {
|
||||
// while (index < arr.length && arr[index][0] <= w) {
|
||||
// queue.add(arr[index][1]);
|
||||
// index++;
|
||||
// }
|
||||
// if (!queue.isEmpty()) {
|
||||
// w += queue.poll();
|
||||
// } else {
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// return w;
|
||||
// }
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
File diff suppressed because one or more lines are too long
46
src/main/java/leetcode/editor/cn/doc/content/Ipo.md
Normal file
46
src/main/java/leetcode/editor/cn/doc/content/Ipo.md
Normal file
@ -0,0 +1,46 @@
|
||||
<p>假设 力扣(LeetCode)即将开始 <strong>IPO</strong> 。为了以更高的价格将股票卖给风险投资公司,力扣 希望在 IPO 之前开展一些项目以增加其资本。 由于资源有限,它只能在 IPO 之前完成最多 <code>k</code> 个不同的项目。帮助 力扣 设计完成最多 <code>k</code> 个不同项目后得到最大总资本的方式。</p>
|
||||
|
||||
<p>给你 <code>n</code> 个项目。对于每个项目 <code>i</code><strong> </strong>,它都有一个纯利润 <code>profits[i]</code> ,和启动该项目需要的最小资本 <code>capital[i]</code> 。</p>
|
||||
|
||||
<p>最初,你的资本为 <code>w</code> 。当你完成一个项目时,你将获得纯利润,且利润将被添加到你的总资本中。</p>
|
||||
|
||||
<p>总而言之,从给定项目中选择 <strong>最多</strong> <code>k</code> 个不同项目的列表,以 <strong>最大化最终资本</strong> ,并输出最终可获得的最多资本。</p>
|
||||
|
||||
<p>答案保证在 32 位有符号整数范围内。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>k = 2, w = 0, profits = [1,2,3], capital = [0,1,1]
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:
|
||||
</strong>由于你的初始资本为 0,你仅可以从 0 号项目开始。
|
||||
在完成后,你将获得 1 的利润,你的总资本将变为 1。
|
||||
此时你可以选择开始 1 号或 2 号项目。
|
||||
由于你最多可以选择两个项目,所以你需要完成 2 号项目以获得最大的资本。
|
||||
因此,输出最后最大化的资本,为 0 + 1 + 3 = 4。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>k = 3, w = 0, profits = [1,2,3], capital = [0,1,2]
|
||||
<strong>输出:</strong>6
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= w <= 10<sup>9</sup></code></li>
|
||||
<li><code>n == profits.length</code></li>
|
||||
<li><code>n == capital.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= profits[i] <= 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= capital[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>贪心</li><li>数组</li><li>排序</li><li>堆(优先队列)</li></div></div><br><div><li>👍 145</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user