leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0870.cpp (651B)


      1 class Solution {
      2   public:
      3     vector<int> advantageCount(vector<int> &nums1, vector<int> &nums2) {
      4         static int indices[100001];
      5         const int n = size(nums1);
      6 
      7         sort(begin(nums1), end(nums1), greater<int>());
      8 
      9         iota(indices, indices + n, 0);
     10         sort(indices, indices + n, [&nums2](int a, int b) { return nums2[a] > nums2[b]; });
     11 
     12         int i = 0, j = 0, k = n - 1;
     13         vector<int> res(n);
     14 
     15         while (i <= k) {
     16             if (nums1[i] > nums2[indices[j]])
     17                 res[indices[j++]] = nums1[i++];
     18             else
     19                 res[indices[j++]] = nums1[k--];
     20         }
     21 
     22         return res;
     23     }
     24 };