leetcode502. 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 several projects. For each projecti, it has a pure profit Pi and a minimum capital of Ci is needed to start the corresponding project. 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.

To sum up, pick a list of at most k distinct projects from given projects to maximize your final capital, and output your final maximized capital.

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.

Note:

1. You may assume all numbers in the input are non-negative integers.
2. The length of Profits array and Capital array will not exceed 50,000.
3. The answer is guaranteed to fit in a 32-bit signed integer.

思路和代码

```public int findMaximizedCapital(int k, int W, int[] Profits, int[] Capital) {
PriorityQueue<Integer> capitalQueue = new PriorityQueue<>(Comparator.comparingInt(o -> Capital[o]));
PriorityQueue<Integer> profitQueue = new PriorityQueue<>((o1, o2) -> Profits[o2] - Profits[o1\]);

IntStream.range(0, Capital.length).forEach(capitalQueue::offer);

while (k-- != 0) {
while (!capitalQueue.isEmpty() &&  Capital[capitalQueue.peek()] <= W) {
profitQueue.offer(capitalQueue.poll());
}
if (profitQueue.isEmpty()) {
break;
}
W += Profits[profitQueue.poll()];
}
return W;
}```

```public int findMaximizedCapital2(int k, int W, int[] Profits, int[] Capital) {
int length = Profits.length;
k = Math.min(k, length);
PriorityQueue<int[]> pq = new PriorityQueue<>(new Comparator<int[]>(){
@Override
public int compare(int[] a, int[] b) {
int p = b[0] - a[0];
if (p == 0) return a[1] - b[1];
return p;
}
});

for (int i = 0; i < length; i++) pq.add(new int[] {Profits[i], Capital[i]});
for (int i = 0; i < k; i++) {
List<int[]> scanned = new ArrayList<>();
int mark = pq.size();
while (pq.size() > 0) {
int[] check = pq.poll();
if (check[1] <= W) {
W += check[0];
break;
} else {
}
}
if (scanned.size() == mark) break;
}

return W;
}```

0 条评论

• 395. Longest Substring with At Least K Repeating Characters

这是一个经典的分治法解决的问题，关键在于我们如何将这个问题分解为更小的子问题。反过来想，这个子字符串一定不包含什么元素呢？当一个元素出现的总次数小于k，那么该元...

• leetcode363. Max Sum of Rectangle No Larger Than K

现有一个由整数构成的矩阵，问从中找到一个子矩阵，要求该子矩阵中各个元素的和为不超过k的最大值，问子矩阵中元素的和为多少? 注：后面的文章中将使用[左上角顶点坐标...

• leetcode480. Sliding Window Median

Median is the middle value in an ordered integer list. If the size of the list i...

• HDUOJ---2642Stars（二维树状数组）

Stars Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/65536 K (Jav...

• HDU 5652 India and China Origins（并查集）

India and China Origins Time Limit: 2000/2000 MS (Java/Others)    Memory Limit:...

• Gym 100952I&&2015 HIAST Collegiate Programming Contest I. Mancala【模拟】

I. Mancala time limit per test：3 seconds memory limit per test：256 megabytes inp...

• 洛谷P2863 [USACO06JAN]牛的舞会The Cow Prom

ng the other ends of her ropes (if she has any), along with the cows holding the...

• POJ 1163（数字三角形）

7 3 8 8 1 0 2 7 4 4 4 5 2 6 5

• 230B. T-primes （数论）

We know that prime numbers are positive integers that have exactly two distinct ...

• Codeforces 833E Caramel Clouds

E. Caramel Clouds time limit per test：3 seconds memory limit per test：256 megaby...