fork download
  1. class Solution {
  2. public:
  3. int minSwaps(vector<int>& nums, vector<int>& forbidden) {
  4. int n = nums.size();
  5.  
  6. // Step 1: Count conflicts and occurrences
  7. unordered_map<int, int> bad; // value -> conflict count
  8. unordered_map<int, int> cntN; // value -> count in nums
  9. unordered_map<int, int> cntF; // value -> count in forbidden
  10.  
  11. for (int i = 0; i < n; i++) {
  12. cntN[nums[i]]++;
  13. cntF[forbidden[i]]++;
  14.  
  15. if (nums[i] == forbidden[i]) {
  16. bad[nums[i]]++;
  17. }
  18. }
  19.  
  20. // Step 2: Check if solution exists
  21. for (auto [val, cnt] : cntN) {
  22. int forbSpots = cntF[val];
  23. int safeSpots = n - forbSpots;
  24.  
  25. if (cnt > safeSpots) return -1;
  26. }
  27.  
  28. // Step 3: Pair up conflicts using max heap
  29. priority_queue<int> pq;
  30. for (auto [val, cnt] : bad) {
  31. pq.push(cnt);
  32. }
  33.  
  34. int ans = 0;
  35.  
  36. // Match two groups at a time
  37. while (pq.size() >= 2) {
  38. int a = pq.top(); pq.pop();
  39. int b = pq.top(); pq.pop();
  40.  
  41. ans++; // One swap fixes both
  42.  
  43. if (a > 1) pq.push(a - 1);
  44. if (b > 1) pq.push(b - 1);
  45. }
  46.  
  47. // Handle leftover conflicts
  48. if (!pq.empty()) {
  49. ans += pq.top();
  50. }
  51.  
  52. return ans;
  53. }
  54. };
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:3:18: error: ‘vector’ has not been declared
     int minSwaps(vector<int>& nums, vector<int>& forbidden) {
                  ^~~~~~
prog.cpp:3:24: error: expected ‘,’ or ‘...’ before ‘<’ token
     int minSwaps(vector<int>& nums, vector<int>& forbidden) {
                        ^
prog.cpp: In member function ‘int Solution::minSwaps(int)’:
prog.cpp:4:17: error: ‘nums’ was not declared in this scope
         int n = nums.size();
                 ^~~~
prog.cpp:4:17: note: suggested alternative: ‘enum’
         int n = nums.size();
                 ^~~~
                 enum
prog.cpp:7:9: error: ‘unordered_map’ was not declared in this scope
         unordered_map<int, int> bad;   // value -> conflict count
         ^~~~~~~~~~~~~
prog.cpp:7:23: error: expected primary-expression before ‘int’
         unordered_map<int, int> bad;   // value -> conflict count
                       ^~~
prog.cpp:8:23: error: expected primary-expression before ‘int’
         unordered_map<int, int> cntN;  // value -> count in nums
                       ^~~
prog.cpp:9:23: error: expected primary-expression before ‘int’
         unordered_map<int, int> cntF;  // value -> count in forbidden
                       ^~~
prog.cpp:12:13: error: ‘cntN’ was not declared in this scope
             cntN[nums[i]]++;
             ^~~~
prog.cpp:12:13: note: suggested alternative: ‘int’
             cntN[nums[i]]++;
             ^~~~
             int
prog.cpp:13:13: error: ‘cntF’ was not declared in this scope
             cntF[forbidden[i]]++;
             ^~~~
prog.cpp:13:13: note: suggested alternative: ‘int’
             cntF[forbidden[i]]++;
             ^~~~
             int
prog.cpp:13:18: error: ‘forbidden’ was not declared in this scope
             cntF[forbidden[i]]++;
                  ^~~~~~~~~
prog.cpp:16:17: error: ‘bad’ was not declared in this scope
                 bad[nums[i]]++;
                 ^~~
prog.cpp:21:19: warning: structured bindings only available with -std=c++17 or -std=gnu++17
         for (auto [val, cnt] : cntN) {
                   ^
prog.cpp:21:32: error: ‘cntN’ was not declared in this scope
         for (auto [val, cnt] : cntN) {
                                ^~~~
prog.cpp:21:32: note: suggested alternative: ‘cnt’
         for (auto [val, cnt] : cntN) {
                                ^~~~
                                cnt
prog.cpp:22:29: error: ‘cntF’ was not declared in this scope
             int forbSpots = cntF[val];
                             ^~~~
prog.cpp:22:29: note: suggested alternative: ‘int’
             int forbSpots = cntF[val];
                             ^~~~
                             int
prog.cpp:29:9: error: ‘priority_queue’ was not declared in this scope
         priority_queue<int> pq;
         ^~~~~~~~~~~~~~
prog.cpp:29:24: error: expected primary-expression before ‘int’
         priority_queue<int> pq;
                        ^~~
prog.cpp:30:19: warning: structured bindings only available with -std=c++17 or -std=gnu++17
         for (auto [val, cnt] : bad) {
                   ^
prog.cpp:30:32: error: ‘bad’ was not declared in this scope
         for (auto [val, cnt] : bad) {
                                ^~~
prog.cpp:31:13: error: ‘pq’ was not declared in this scope
             pq.push(cnt);
             ^~
prog.cpp:37:16: error: ‘pq’ was not declared in this scope
         while (pq.size() >= 2) {
                ^~
prog.cpp:48:14: error: ‘pq’ was not declared in this scope
         if (!pq.empty()) {
              ^~
stdout
Standard output is empty