diff --git a/502_ipo.cpp b/502_ipo.cpp new file mode 100644 index 0000000..d4ebd77 --- /dev/null +++ b/502_ipo.cpp @@ -0,0 +1,46 @@ +#include "test.h" + + +class Solution { +public: + int findMaximizedCapital(int k, int w, vector& profits, vector& capital) + { + std::multimap> 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, vector>> 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; +} diff --git a/Makefile b/Makefile index b43a8f6..4ff0dc0 100644 --- a/Makefile +++ b/Makefile @@ -14,6 +14,7 @@ targets = \ 13_roman_to_integer.exec\ 14_longest_common_prefix.exec\ 15_3_sum.exec\ + 502_ipo.exec\ CC = g++ diff --git a/ostream_stdcontainer.h b/ostream_stdcontainer.h index 5c8c4d4..d7edee7 100644 --- a/ostream_stdcontainer.h +++ b/ostream_stdcontainer.h @@ -22,6 +22,8 @@ #include #include +#include + #include using std::cout; diff --git a/test.h b/test.h index 89ea89f..c60e83e 100644 --- a/test.h +++ b/test.h @@ -46,6 +46,8 @@ using std::multiset; using std::unordered_set; using std::unordered_multiset; +using std::tuple; + using namespace std::placeholders; #endif