IPO 🚧

Problem

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
int findMaximizedCapital(int k, int W, vector<int>& Profits, vector<int>& Capital) {
int len = Profits.size();
unordered_set<int> selected;
for (int i = 0; i < k; i++) {
// pair of project index and corresponding profit
pair<int, int> best = make_pair(0,0);
for (int j = 0; j < len; j++) {
// if available capital is enough to pick project and project not picked already
if (W >= Capital[j]) {
if (selected.find(j) == selected.end()) {
if (best.second < Profits[j]) {
best = make_pair(j, Profits[j]);
}
}
}
}
selected.insert(best.first);
W += Profits[best.first];
}
return W;
}
};

We need to ensure we check if best,first

Second Attempt

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
class Solution {
public:
int findMaximizedCapital(int k, int W, vector<int>& Profits, vector<int>& Capital) {
int len = Profits.size();
unordered_set<int> selected;
for (int i = 0; i < k; i++) {
// pair of project index and corresponding profit
pair<int, int> best = make_pair(-1,-1);
for (int j = 0; j < len; j++) {
// if available capital is enough to pick project and project not picked already
if (W >= Capital[j]) {
if (selected.find(j) == selected.end()) {
if (best.second < Profits[j]) {
best = make_pair(j, Profits[j]);
}
}
}
}
if (best.first >= 0) {
selected.insert(best.first);
W += Profits[best.first];
}
}
return W;
}
};

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
33
class 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
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
33
34
35
36
37
38
39
40
#include <bits/stdc++.h>

class 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;
});
//cout << "Hello\n";
for (int i = 0; i < k; i++) {
vector<elem_t>::iterator it = v.begin();
vector<elem_t>::iterator selected = v.end();
for (; it != v.end(); it++) {
if ((*it).c <= W) {
//cout << "Got cost " << (*it).c << endl;
selected = it;
break;
}
}

if (selected != v.end()) {
//cout << "got " << (*selected).p << endl;
W += (*selected).p;
v.erase(selected);
}
}
return W;
}
};

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
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
33
34
35
36
37
38
39
40


class Solution {
struct elem_t {
int id;
int p; //profit
int c; //capital
//bool operator < (const elem_t &other) const { return p < other.p; }
};
public:
int findMaximizedCapital(int k, int W, vector<int>& Profits, vector<int>& Capital) {
int len = Profits.size();
vector<elem_t> v(len);
unordered_set<int> picked;
for (int i = 0; i < len; i++) {
v[i] = {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++) {
bool selected = false;
int j = 0;
for (; j < v.size(); j++) {
if ((v[j].c <= W) && (picked.find(j) == picked.end())) {
selected = true;
break;
}
}

if (selected) {
cout << "got " << v[j].p << endl;
W += v[j].p;
picked.insert(j);
}
}
return W;
}
};

Um….this one times out! 😰
So, using the set is not as helpful as I thought.