add: 220525 [cpp]

This commit is contained in:
Eat-Swap 2022-05-26 20:21:08 +08:00
parent 02bf5f38a3
commit b01dc3baf5
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
2 changed files with 30 additions and 1 deletions

29
cpp/2205/220525.cpp Normal file
View File

@ -0,0 +1,29 @@
#include <vector>
#include <algorithm>
/**
* 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<std::vector<int>>& e) {
std::sort(e.begin(), e.end(), [](const std::vector<int>& x, const std::vector<int>& y) {
return (x[0] == y[0]) ? (x[1] > y[1]) : (x[0] < y[0]);
});
std::vector<int> ans;
std::for_each(e.begin(), e.end(), [&](const std::vector<int>& 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();
}
};

View File

@ -3,4 +3,4 @@ PROJECT(2205)
SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2205 220524.cpp)
ADD_EXECUTABLE(2205 220525.cpp)