mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-14 01:49:41 +01:00
34 lines
793 B
C++
34 lines
793 B
C++
#include <map>
|
|
#include <vector>
|
|
|
|
class Solution {
|
|
public:
|
|
std::vector<std::vector<int>> findMatrix(const std::vector<int> &nums) {
|
|
// 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;
|
|
for (auto &[num, count] : freqs) {
|
|
if (count <= 0) {
|
|
continue;
|
|
}
|
|
|
|
--count;
|
|
row.push_back(num);
|
|
elements_left = elements_left || count > 0;
|
|
}
|
|
matrix.push_back(row);
|
|
}
|
|
|
|
return matrix;
|
|
}
|
|
};
|