diff --git a/cpp/2205/220525.cpp b/cpp/2205/220525.cpp new file mode 100644 index 0000000..26e8585 --- /dev/null +++ b/cpp/2205/220525.cpp @@ -0,0 +1,29 @@ +#include +#include + +/** + * 354. Russian Doll Envelopes + * You are given a 2D array of integers envelopes where envelopes[i] = [wi, hi] represents the width and the height of an envelope. + * One envelope can fit into another if and only if both the width and height of one envelope are greater than the other envelope's width and height. + * Return the maximum number of envelopes you can Russian doll (i.e., put one inside the other). + * Note: You cannot rotate an envelope. + */ + +class Solution { +public: + static int maxEnvelopes(std::vector>& e) { + std::sort(e.begin(), e.end(), [](const std::vector& x, const std::vector& y) { + return (x[0] == y[0]) ? (x[1] > y[1]) : (x[0] < y[0]); + }); + std::vector ans; + std::for_each(e.begin(), e.end(), [&](const std::vector& x) { + // Put this into list + int idx = std::lower_bound(ans.begin(), ans.end(), x[1]) - ans.begin(); + if (idx >= ans.size()) + ans.push_back(x[1]); + else + ans[idx] = x[1]; + }); + return ans.size(); + } +}; diff --git a/cpp/2205/CMakeLists.txt b/cpp/2205/CMakeLists.txt index d441493..f7df0ea 100644 --- a/cpp/2205/CMakeLists.txt +++ b/cpp/2205/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2205) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2205 220524.cpp) +ADD_EXECUTABLE(2205 220525.cpp)