반응형
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/
Group the People Given the Group Size They Belong To - LeetCode
Can you solve this real interview question? Group the People Given the Group Size They Belong To - There are n people that are split into some unknown number of groups. Each person is labeled with a unique ID from 0 to n - 1. You are given an integer
leetcode.com
같은 크기의 그룹 사람들을 묶어주고, 주어진 크기가 되면 그루핑해줍니다.
class Solution {
public:
vector<int> grp[501];
vector<vector<int>> groupThePeople(vector<int>& groupSizes) {
vector<vector<int>> res;
for(int i=0; i<groupSizes.size(); i++) {
int sz = groupSizes[i];
grp[sz].push_back(i);
if(grp[sz].size() == sz) {
res.push_back(grp[sz]);
grp[sz].clear();
}
}
return res;
}
};
반응형
'Algorithm' 카테고리의 다른 글
LeetCode 847. Shortest Path Visiting All Nodes (1) | 2023.09.17 |
---|---|
LeetCode 1631. Path With Minimum Effort (0) | 2023.09.16 |
LeetCode 377. Combination Sum IV (0) | 2023.09.09 |
LeetCode 118. Pascal's Triangle (0) | 2023.09.09 |
LeetCode 2707. Extra Characters in a String (0) | 2023.09.02 |