2024-01-02 18:47:37 +01:00
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
class Solution {
|
2024-01-03 12:06:42 +01:00
|
|
|
public:
|
|
|
|
std::vector<std::vector<int>> findMatrix(const std::vector<int> &nums) {
|
2024-01-02 18:47:37 +01:00
|
|
|
// count the numbers
|
|
|
|
std::map<int, int> freqs;
|
|
|
|
for (auto x : nums) {
|
|
|
|
++freqs[x];
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::vector<int>> matrix;
|
|
|
|
|
|
|
|
bool elements_left = true;
|
|
|
|
while (elements_left) {
|
|
|
|
elements_left = false;
|
|
|
|
|
|
|
|
std::vector<int> row;
|
2024-01-03 12:06:42 +01:00
|
|
|
for (auto &[num, count] : freqs) {
|
2024-01-02 18:47:37 +01:00
|
|
|
if (count <= 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
--count;
|
|
|
|
row.push_back(num);
|
|
|
|
elements_left = elements_left || count > 0;
|
|
|
|
}
|
|
|
|
matrix.push_back(row);
|
|
|
|
}
|
|
|
|
|
|
|
|
return matrix;
|
|
|
|
}
|
|
|
|
};
|