From 6056527c97ad21c8ce924861cf691ff69666b044 Mon Sep 17 00:00:00 2001 From: Manuel Pineda Date: Sat, 26 Dec 2020 15:07:54 +0100 Subject: [PATCH] Add LeetCode 1282. --- .../solution.cc | 37 ++++++++++++ .../template.cc | 58 +++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 solved/LeetCode/1282. Group the People Given the Group Size They Belong To/solution.cc create mode 100644 solved/LeetCode/1282. Group the People Given the Group Size They Belong To/template.cc diff --git a/solved/LeetCode/1282. Group the People Given the Group Size They Belong To/solution.cc b/solved/LeetCode/1282. Group the People Given the Group Size They Belong To/solution.cc new file mode 100644 index 00000000..8588675a --- /dev/null +++ b/solved/LeetCode/1282. Group the People Given the Group Size They Belong To/solution.cc @@ -0,0 +1,37 @@ +#include + +using namespace std; + +#include "template.cc" + +class Solution { +public: + vector> groupThePeople(vector& groupSizes) { + vector> data; + for (int i = 0; i < groupSizes.size(); i++) { + data.emplace_back(groupSizes[i], i); + } + sort(data.begin(), data.end()); + vector> ans; + vector cur; + for (auto it: data) { + debug(it); + cur.push_back(it.second); + if (cur.size() + 1 > it.first) { + ans.push_back(cur); + cur = {}; + } + } + if (!cur.empty()) { + ans.push_back(cur); + } + return ans; + } +}; + +int main() { + Solution sol; + // vector groupSizes = {3,3,3,3,3,1,3}; + vector groupSizes = {2,1,3,3,3,2}; + debug(sol.groupThePeople(groupSizes)); +} diff --git a/solved/LeetCode/1282. Group the People Given the Group Size They Belong To/template.cc b/solved/LeetCode/1282. Group the People Given the Group Size They Belong To/template.cc new file mode 100644 index 00000000..cc3ea288 --- /dev/null +++ b/solved/LeetCode/1282. Group the People Given the Group Size They Belong To/template.cc @@ -0,0 +1,58 @@ +void print(std::ostream& out) { out << std::endl; } + +template +void printOne(std::ostream& out, const std::pair& a) { + out << "{" << a.first << ", " << a.second << "}"; +} + +template +void printOne(std::ostream& out, const T& a) { + out << a; +} + +template +void printOne(std::ostream& out, const std::vector& a) { + out << "{"; + for (int i = 0; i < a.size(); i++) { + if (i) out << ", "; + printOne(out, a[i]); + } + out << "}"; +} + +template +void printOne(std::ostream& out, const std::deque& a) { + out << "{"; + for (int i = 0; i < a.size(); i++) { + if (i) out << ", "; + printOne(out, a[i]); + } + out << "}"; +} + + +template +void printOne(std::ostream& out, const std::map& a) { + out << "{"; + for (auto [key, val] : a) { + out << "["; printOne(out, key); out << " = "; printOne(out, val); out << "],"; + } + out << "}"; +} + + +template +void print(std::ostream& out, const T& first, const Tail... tail) { + printOne(out, first); + if (sizeof...(tail) != 0) { + out << ", "; + } + print(out, tail...); +} + +#ifdef LOCAL_CP +#define debug(...) \ + std::cerr << "[DEBUG] [" << #__VA_ARGS__ << "] = ", print(std::cerr, __VA_ARGS__) +#else +#define debug(...) 0 +#endif