-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.cpp
47 lines (41 loc) · 1014 Bytes
/
code.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <vector>
using namespace std;
class Solution
{
public:
vector<vector<int>> findMatrix(vector<int>& nums)
{
int n = nums.size();
vector<int> visited(n , 0);
for (int i=0 ; i<n ; i++)
{
visited[nums[i]-1]++;
}
int largest = 0;
for (int i = 0; i < n; i++)
{
if(visited[i]>largest)
{
largest = visited[i];
}
}
vector<vector<int>> ans;
vector<int> arr;
for (int i = 0; i < largest; i++)
{
arr.clear();
for (int j = 0; j < n; j++)
{
if(visited[j]>0)
{
arr.push_back(j+1);
visited[j]--;
}
}
ans.push_back(arr);
}
return ans;
}
};
// This code can further be optimised a little bit by implementing the same logic but by using unordered map