class Solution { public: vector groupThePeople(vector& groupSizes) { vector groups; unordered_map groupMap; // Iterate through the groupSizes vector for (int i = 0; i < groupSizes.size(); i++) { int size = groupSizes[i];
// Add the person to the corresponding group based on their group size groupMap[size].push_back(i);
// If the group size is equal to the number of people in the group, add it to the result if (groupMap[size].size() == size) { groups.push_back(groupMap[size]); groupMap[size].clear(); // Clear the group } }
Connect the strings, but not it a literal way.
Lovely❤😊
Thank you! ♥️
class Solution {
public:
vector groupThePeople(vector& groupSizes) {
vector groups;
unordered_map groupMap;
// Iterate through the groupSizes vector
for (int i = 0; i < groupSizes.size(); i++) {
int size = groupSizes[i];
// Add the person to the corresponding group based on their group size
groupMap[size].push_back(i);
// If the group size is equal to the number of people in the group, add it to the result
if (groupMap[size].size() == size) {
groups.push_back(groupMap[size]);
groupMap[size].clear(); // Clear the group
}
}
return groups;
}
};
Nice