-
Notifications
You must be signed in to change notification settings - Fork 0
2610_ConvertanArrayIntoa2DArrayWithConditions
a920604a edited this page Apr 14, 2023
·
1 revision
categories: leetcode comments: false tags: -- hash table title: 2610. Convert an Array Into a 2D Array With Conditions
class Solution {
public:
vector<vector<int>> findMatrix(vector<int>& nums) {
int n = nums.size();
vector<int> collects(n+1, 0);
for(int n:nums) collects[n]++;
vector<vector<int>> rets;
while(n>0) {
vector<int> tmp;
for(int i=1;i<collects.size() ;++i){
if( collects[i] > 0) {
collects[i]--;
n--;
tmp.push_back(i);
}
}
if(!tmp.empty()) rets.push_back(tmp);
}
return rets;
}
};
- time complexity
O(mk)
m is length of nums , k is max number range - space complexity
O(k)
footer