1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-09 15:59:06 +01:00

cpp: add “2610. Convert an Array Into a 2D Array With Conditions”

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-01-02 18:47:37 +01:00
parent 9af5c9e61e
commit f8f23de17e
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -0,0 +1,35 @@
#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;
}
};