Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- string canAllocateTasks(int K, vector<int> taskIds) {
- // Create a frequency map of taskIds
- unordered_map<int, int> freq;
- for (int id : taskIds) {
- freq[id]++;
- }
- // Check if there are more than K distinct task IDs than can be handled at once
- if (freq.size() < K) {
- return "No";
- }
- // Count how many groups can be formed
- vector<int> frequencies;
- for (const auto& entry : freq) {
- frequencies.push_back(entry.second);
- }
- // Sort frequencies in descending order
- sort(frequencies.rbegin(), frequencies.rend());
- // Check if we can group the task IDs in valid distributions
- int groups = 0;
- int currentGroup = 0;
- for (int i = 0; i < frequencies.size(); i++) {
- // Each group requires K distinct task IDs
- if (currentGroup < K) {
- currentGroup++;
- groups++;
- } else {
- return "No"; // Return No if it's not possible to make a valid distribution
- }
- }
- return "Yes";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement