From e316b450e65d85622316772006790aad4724e21f Mon Sep 17 00:00:00 2001 From: Eat-Swap Date: Fri, 1 Jul 2022 01:49:07 +0800 Subject: [PATCH] add: 220629 --- cpp/2206/220629.cpp | 22 ++++++++++++++++++++++ cpp/2206/CMakeLists.txt | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 cpp/2206/220629.cpp diff --git a/cpp/2206/220629.cpp b/cpp/2206/220629.cpp new file mode 100644 index 0000000..0095a6f --- /dev/null +++ b/cpp/2206/220629.cpp @@ -0,0 +1,22 @@ +#include +#include + +/** + * 406. Queue Reconstruction by Height + * You are given an array of people, people, which are the attributes of some people in a queue (not necessarily in order). Each people[i] = [hi, ki] represents the ith person of height hi with exactly ki other people in front who have a height greater than or equal to hi. + * Reconstruct and return the queue that is represented by the input array people. The returned queue should be formatted as an array queue, where queue[j] = [hj, kj] is the attributes of the jth person in the queue (queue[0] is the person at the front of the queue). + */ + +class Solution { +public: + static std::vector> reconstructQueue(std::vector>& people) { + std::sort(people.begin(), people.end(), [](const auto& x, const auto& y) { + return (x[0] == y[0]) ? (x[1] < y[1]) : (x[0] > y[0]); + }); + std::vector> ret; + ret.reserve(people.size()); + for (const auto& i : people) + ret.insert(ret.begin() + i[1], i); + return ret; + } +}; diff --git a/cpp/2206/CMakeLists.txt b/cpp/2206/CMakeLists.txt index 3ec16fe..573f5c8 100644 --- a/cpp/2206/CMakeLists.txt +++ b/cpp/2206/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2206) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2206 220628.cpp) +ADD_EXECUTABLE(2206 220629.cpp)