-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
solved/LeetCode/1282. Group the People Given the Group Size They Belong To/solution.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
58 changes: 58 additions & 0 deletions
58
solved/LeetCode/1282. Group the People Given the Group Size They Belong To/template.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |