Skip to content

Commit 6c514ce

Browse files
committed
502
1 parent 33108c2 commit 6c514ce

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

502_ipo.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "test.h"
2+
3+
4+
class Solution {
5+
public:
6+
int findMaximizedCapital(int k, int w, vector<int>& profits, vector<int>& capital)
7+
{
8+
std::multimap<int, int, std::greater<int>> profits2captial;
9+
for (auto i = 0u; i < profits.size(); ++i)
10+
profits2captial.insert({profits[i], capital[i]});
11+
12+
while (k > 0)
13+
{
14+
int oldK = k;
15+
for (auto iter = profits2captial.begin(); iter != profits2captial.end(); ++iter)
16+
{
17+
if (iter->second > w)
18+
continue;
19+
20+
w += iter->first;
21+
profits2captial.erase(iter);
22+
--k;
23+
break;
24+
}
25+
26+
if (oldK == k)
27+
break;
28+
}
29+
30+
return w;
31+
}
32+
};
33+
34+
35+
int main()
36+
{
37+
vector<tuple<int, int, vector<int>, vector<int>>> datas =
38+
{
39+
{2, 0, {1, 2, 3}, {0, 1, 1}},
40+
{1, 0, {1, 2, 3}, {1, 1, 2}},
41+
};
42+
Solution s;
43+
for (auto& data : datas)
44+
cout << s.findMaximizedCapital(std::get<0>(data), std::get<1>(data), std::get<2>(data), std::get<3>(data)) << endl;
45+
return 0;
46+
}

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ targets = \
1414
13_roman_to_integer.exec\
1515
14_longest_common_prefix.exec\
1616
15_3_sum.exec\
17+
502_ipo.exec\
1718

1819

1920
CC = g++

ostream_stdcontainer.h

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include <list>
2323
#include <forward_list>
2424

25+
#include <tuple>
26+
2527
#include <iostream>
2628

2729
using std::cout;

test.h

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ using std::multiset;
4646
using std::unordered_set;
4747
using std::unordered_multiset;
4848

49+
using std::tuple;
50+
4951
using namespace std::placeholders;
5052

5153
#endif

0 commit comments

Comments
 (0)