Skip to content

Commit

Permalink
Add LeetCode 1282.
Browse files Browse the repository at this point in the history
  • Loading branch information
pin3da committed Dec 26, 2020
1 parent af9f218 commit 6056527
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <bits/stdc++.h>

using namespace std;

#include "template.cc"

class Solution {
public:
vector<vector<int>> groupThePeople(vector<int>& groupSizes) {
vector<pair<int, int>> data;
for (int i = 0; i < groupSizes.size(); i++) {
data.emplace_back(groupSizes[i], i);
}
sort(data.begin(), data.end());
vector<vector<int>> ans;
vector<int> 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<int> groupSizes = {3,3,3,3,3,1,3};
vector<int> groupSizes = {2,1,3,3,3,2};
debug(sol.groupThePeople(groupSizes));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
void print(std::ostream& out) { out << std::endl; }

template <typename T, typename T2>
void printOne(std::ostream& out, const std::pair<T, T2>& a) {
out << "{" << a.first << ", " << a.second << "}";
}

template <typename T>
void printOne(std::ostream& out, const T& a) {
out << a;
}

template <typename T>
void printOne(std::ostream& out, const std::vector<T>& a) {
out << "{";
for (int i = 0; i < a.size(); i++) {
if (i) out << ", ";
printOne(out, a[i]);
}
out << "}";
}

template <typename T>
void printOne(std::ostream& out, const std::deque<T>& a) {
out << "{";
for (int i = 0; i < a.size(); i++) {
if (i) out << ", ";
printOne(out, a[i]);
}
out << "}";
}


template <typename T, typename V>
void printOne(std::ostream& out, const std::map<T, V>& a) {
out << "{";
for (auto [key, val] : a) {
out << "["; printOne(out, key); out << " = "; printOne(out, val); out << "],";
}
out << "}";
}


template <typename T, typename... Tail>
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

0 comments on commit 6056527

Please sign in to comment.