Skip to content

Commit

Permalink
502
Browse files Browse the repository at this point in the history
  • Loading branch information
retaw committed Sep 5, 2018
1 parent 33108c2 commit 6c514ce
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
46 changes: 46 additions & 0 deletions 502_ipo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "test.h"


class Solution {
public:
int findMaximizedCapital(int k, int w, vector<int>& profits, vector<int>& capital)
{
std::multimap<int, int, std::greater<int>> profits2captial;
for (auto i = 0u; i < profits.size(); ++i)
profits2captial.insert({profits[i], capital[i]});

while (k > 0)
{
int oldK = k;
for (auto iter = profits2captial.begin(); iter != profits2captial.end(); ++iter)
{
if (iter->second > w)
continue;

w += iter->first;
profits2captial.erase(iter);
--k;
break;
}

if (oldK == k)
break;
}

return w;
}
};


int main()
{
vector<tuple<int, int, vector<int>, vector<int>>> datas =
{
{2, 0, {1, 2, 3}, {0, 1, 1}},
{1, 0, {1, 2, 3}, {1, 1, 2}},
};
Solution s;
for (auto& data : datas)
cout << s.findMaximizedCapital(std::get<0>(data), std::get<1>(data), std::get<2>(data), std::get<3>(data)) << endl;
return 0;
}
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ targets = \
13_roman_to_integer.exec\
14_longest_common_prefix.exec\
15_3_sum.exec\
502_ipo.exec\


CC = g++
Expand Down
2 changes: 2 additions & 0 deletions ostream_stdcontainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include <list>
#include <forward_list>

#include <tuple>

#include <iostream>

using std::cout;
Expand Down
2 changes: 2 additions & 0 deletions test.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ using std::multiset;
using std::unordered_set;
using std::unordered_multiset;

using std::tuple;

using namespace std::placeholders;

#endif
Expand Down

0 comments on commit 6c514ce

Please sign in to comment.