Advertisement
Protyay77

Untitled

Apr 13th, 2025 (edited)
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | Source Code | 0 0
  1. string canAllocateTasks(int K, vector<int> taskIds) {
  2.     // Create a frequency map of taskIds
  3.     unordered_map<int, int> freq;
  4.    
  5.     for (int id : taskIds) {
  6.         freq[id]++;
  7.     }
  8.    
  9.     // Check if there are more than K distinct task IDs than can be handled at once
  10.     if (freq.size() < K) {
  11.         return "No";
  12.     }
  13.    
  14.     // Count how many groups can be formed
  15.     vector<int> frequencies;
  16.     for (const auto& entry : freq) {
  17.         frequencies.push_back(entry.second);
  18.     }
  19.    
  20.     // Sort frequencies in descending order
  21.     sort(frequencies.rbegin(), frequencies.rend());
  22.    
  23.     // Check if we can group the task IDs in valid distributions
  24.     int groups = 0;
  25.     int currentGroup = 0;
  26.    
  27.     for (int i = 0; i < frequencies.size(); i++) {
  28.         // Each group requires K distinct task IDs
  29.         if (currentGroup < K) {
  30.             currentGroup++;
  31.             groups++;
  32.         } else {
  33.             return "No";  // Return No if it's not possible to make a valid distribution
  34.         }
  35.     }
  36.    
  37.     return "Yes";
  38. }
  39.  
Tags: Protyay_q1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement