leetcode

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

2563.cpp (517B)


      1 class Solution {
      2     static long long countLess(const vector<int> &nums, int target) {
      3         long long res = 0;
      4 
      5         for (int i = 0, j = size(nums) - 1; i < j; i++) {
      6             while (i < j && nums[i] + nums[j] > target)
      7                 j--;
      8             res += j - i;
      9         }
     10 
     11         return res;
     12     }
     13 
     14   public:
     15     long long countFairPairs(vector<int> &nums, int lower, int upper) const {
     16         sort(begin(nums), end(nums));
     17         return countLess(nums, upper) - countLess(nums, lower - 1);
     18     }
     19 };