-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6363.cpp
61 lines (55 loc) · 1.2 KB
/
6363.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <string>
#include <vector>
#include <iostream>
#include <unordered_map>
#include <algorithm>
#include <unordered_set>
using namespace std;
class Solution {
public:
vector<vector<int>> findMatrix(vector<int>& nums) {
vector<vector<int>> ret;
unordered_map<int, int> hash;
unordered_set<int> collect;
for (auto nu : nums) {
hash[nu] = hash[nu] + 1;
collect.insert(nu);
}
while (!collect.empty()) {
vector<int> row;
for (auto i = hash.begin(); i != hash.end(); ++i) {
if (i->second == 0) {
continue;
} else {
row.push_back(i->first);
i->second = i->second - 1;
if (i->second == 0) {
collect.erase(i->first);
}
}
}
ret.push_back(row);
}
return ret;
}
};
int main(int argc, char const* argv[]) {
Solution s;
vector<int> v1 = {1, 3, 4, 1, 2, 3, 1};
vector<int> v2 = {1, 2, 3, 4};
auto res1 = s.findMatrix(v1);
for (auto r : res1) {
for (auto c : r) {
cout << c << " ";
}
cout << endl;
}
auto res2 = s.findMatrix(v2);
for (auto r : res2) {
for (auto c : r) {
cout << c << " ";
}
cout << endl;
}
return 0;
}