*************
C++
topic:354. 俄罗斯套娃信封问题 - 力扣(LeetCode)
*************
Monday again and the last weekend is not beautiful. I broke my sunglass on the way back home. After fill up the oil tank, I opened the door and just popped the sunglasses out. Fortunately, my ass is still fine.
Inspect the topic:

The point of this topic is Size Comparison of Two-dimensional Array. I've done the trible one like this: 动态规划 - 堆箱子 - 困难 - 再尝试_boxes = [details['left'][i], details['top'][i], de-CSDN博客。
To solve projects like this, order always works.

However, I donot use find function in this topic. The gif tells that order first when deal with array problems.
So order first. Sort by width in ascending order, if width is the same, sort by height in descending order. Why? Cuz envelopes[i] = [wi, hi], wi is the first. and why sort by height in descending order? Think about if I got [[5,4],[6,4],[6,7],[2,3]] four envelopes, aort by width is [[2,3],[5,4],[6,7],[6,4]], if having the same width, envelopes with higher heights will not be mistaken for envelopes that can contain lower heights.
Go over the usage of sort.

This gay teaches very well, and I want to share him.
Sort is a standard library in c++, used to sort elements in an array or container.
Review the usage of sort:
the basic usage is as follow:
sort(ForwardIterator first, ForwardIterator last);
sort defaults to ascending order.
if we donot give sort-code the role, then it will Sort the array in ascending order.
#include <algorithm>
#include <iostream>
#include <vector>
int main()
{
std::vector<int> vec = {5, 3, 2, 4, 1};
std::sort(vec.begin(), vec.end());
for (int num : vec)
{
std::cout << num << " ";
}
return 0;
}
and the output is :

Suppose you want to sort the array in descending order. Change the code a little bit.
#include <algorithm>
#include <iostream>
#include <vector>
bool compare(int a, int b)
{
return a > b; // 降序排序
}
int main()
{
std::vector<int> vec = {5, 3, 2, 4, 1};
std::sort(vec.begin(), vec.end());
for (int num : vec)
{
std::cout << num << " ";
}
return 0;
}
then the return is as follow:

and what if I want 5 laies in the first place?
do this:
#include <algorithm>
#include <vector>
#include <iostream>
// 自定义比较函数,将5放在最前面,其他数字升序排列
bool compare(int a, int b) {
if (a == 5) return true; // 如果a是5,总是返回true,使其排在前面
if (b == 5) return false; // 如果b是5,总是返回false,使其排在前面
return a < b; // 其他数字按升序排列
}
int main() {
std::vector<int> vec = {5, 3, 2, 4, 1};
std::sort(vec.begin(), vec.end(), compare);
for (int num : vec) {
std::cout << num << " ";
}
return 0;
}
and the return is :

use sort in this project:
class Solution {
public:
int maxEnvelopes(vector<vector<int>>& envelopes) {
// 按宽度升序排序,如果宽度相同,按高度降序排序
sort(envelopes.begin(), envelopes.end(), [](const vector<int>& a, const vector<int>& b) {
if (a[0] == b[0]) {
return a[1] > b[1];
} else {
return a[0] < b[0];
}
});
}
};
and inspect the topic, output a number. So just find the heigth.
class Solution {
public:
int maxEnvelopes(vector<vector<int>>& envelopes) {
// 按宽度升序排序,如果宽度相同,按高度降序排序
sort(envelopes.begin(), envelopes.end(), [](const vector<int>& a, const vector<int>& b) {
if (a[0] == b[0]) {
return a[1] > b[1];
} else {
return a[0] < b[0];
}
});
// 提取高度
vector<int> heights;
for (const auto& env : envelopes) {
heights.push_back(env[1]);
}
then count. something familar comes to me:最长递增子序列的个数 - 中等难度_给定一个长度为 n 的数列,求数值严格单调递增的子序列的长度最长是多少。输入格式-CSDN博客
class Solution {
public:
int maxEnvelopes(vector<vector<int>>& envelopes) {
// 按宽度升序排序,如果宽度相同,按高度降序排序
sort(envelopes.begin(), envelopes.end(), [](const vector<int>& a, const vector<int>& b) {
if (a[0] == b[0]) {
return a[1] > b[1];
} else {
return a[0] < b[0];
}
});
// 提取高度
vector<int> heights;
for (const auto& env : envelopes) {
heights.push_back(env[1]);
}
// 寻找最长递增子序列
vector<int> dp;
for (int h : heights) {
if (dp.empty() || h > dp.back()) {
dp.push_back(h);
} else {
// 找到第一个大于等于h的位置,并替换
auto it = lower_bound(dp.begin(), dp.end(), h);
*it = h;
}
}
return dp.size();
}
};
and that's it.


被折叠的 条评论
为什么被折叠?



