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 project i, 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:
You may assume all numbers in the input are non-negative integers.
The length of Profits array and Capital array will not exceed 50,000.
The answer is guaranteed to fit in a 32-bit signed integer.
Analysis
We would like to maximize the profit of each investment at each step to get the best overall profit. This suggests a greedy approach.
First Attempt
1 | class Solution { |
We need to ensure we check if best,first
Second Attempt
1 | class Solution { |
Time limited exceeded on this solution. ⏲
How can we optimize this?
##1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33class Solution {
struct elem_t {
int p; //profit
int c; //capital
};
public:
int findMaximizedCapital(int k, int W, vector<int>& Profits,
vector<int>& Capital) {
int len = Profits.size();
vector<elem_t> v(len);
for (int i = 0; i < len; i++) {
v[i] = {Profits[i], Capital[i]};
}
sort(v.begin(), v.end(),
[](const elem_t& a, const elem_t& b) {
return a.p > b.p;
});
for (int i = 0; i < k; i++) {
auto it = v.begin();
auto selected = it;
for (; it != v.end(); it++) {
if ((*it).c <= W) {
selected = it;
break;
}
}
W += (*selected).p;
v.erase(selected);
}
return W;
}
};
Accepted non-optimal solution
1 |
|
The inefficiency is a result of us using erase()
as seen here
“Because vectors use an array as their underlying storage, erasing elements in positions other
than the vector end causes the container to relocate all the elements after the segment
erased to their new positions. This is generally an inefficient operation compared to the one
performed for the same operation by other kinds of sequence containers (such as list or
forward_list).”
I was afraid of that! Guess checking documentation is a good idea. 😖
Let’s try to use a set to track what projects we have already selected. We can track via addresses of array elements.
1 |
|
Um….this one times out! 😰
So, using the set is not as helpful as I thought.