Problem solution · Python

Number of Pairs Satisfying Inequality

Number of Pairs Satisfying Inequality: a Python solution using segment tree or range structure. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Segment tree or range structure
Source
Kamyu LeetCode Solutions
Length
109 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Segment tree or range structure

For Number of Pairs Satisfying Inequality, the implementation stores interval information in a range-query data structure so updates and queries avoid rescanning the full input.

  1. Choose the aggregate stored for each interval or prefix.
  2. Build or initialize the structure from the input.
  3. Apply updates and combine the affected nodes to answer each query.

Code notes

  • 109 lines of Python from the credited upstream file number-of-pairs-satisfying-inequality.py.
  • The implementation visibly relies on sequence storage, ordered lookup.
  • No explicit loop blocks detected.

Complexity

Count the build once, then multiply the logarithmic update or query path by the number of operations.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeNumber of Pairs Satisfying Inequality · PythonPython
Use this to learn the idea, then write your own version.
# Time:  O(nlogn)# Space: O(n) from sortedcontainers import SortedListimport itertools  # sorted list, binary searchclass Solution(object):    def numberOfPairs(self, nums1, nums2, diff):        """        :type nums1: List[int]        :type nums2: List[int]        :type diff: int        :rtype: int        """        sl = SortedList()        result = 0        for x, y in itertools.izip(nums1, nums2):            result += sl.bisect_right((x-y)+diff)            sl.add(x-y)        return result     # Time:  O(nlogn)# Space: O(n)import itertoolsimport bisect  class BIT(object):  # 0-indexed.    def __init__(self, n):        self.__bit = [0]*(n+1)  # Extra one for dummy node.     def add(self, i, val):        i += 1  # Extra one for dummy node.        while i < len(self.__bit):            self.__bit[i] += val            i += (i & -i)     def query(self, i):        i += 1  # Extra one for dummy node.        ret = 0        while i > 0:            ret += self.__bit[i]            i -= (i & -i)        return ret  # bit, fenwick tree, coordinate compressionclass Solution2(object):    def numberOfPairs(self, nums1, nums2, diff):        """        :type nums1: List[int]        :type nums2: List[int]        :type diff: int        :rtype: int        """        sorted_nums = sorted(set(x-y for x, y in itertools.izip(nums1, nums2)))        num_to_idx = {x:i for i, x in enumerate(sorted_nums)}        result = 0        bit = BIT(len(num_to_idx))        for x, y in itertools.izip(nums1, nums2):            result += bit.query(bisect.bisect_right(sorted_nums, (x-y)+diff)-1)            bit.add(num_to_idx[x-y], 1)        return result  # Time:  O(nlogn)# Space: O(n)import itertools  # merge sort, two pointersclass Solution3(object):    def numberOfPairs(self, nums1, nums2, diff):        """        :type nums1: List[int]        :type nums2: List[int]        :type diff: int        :rtype: int        """        def merge_sort(nums, left, right, result):            if left == right:                return            mid = left+(right-left)//2            merge_sort(nums, left, mid, result)            merge_sort(nums, mid+1, right, result)            r = mid+1            for l in xrange(left, mid+1):                while r < right+1 and nums[l]-nums[r] > diff:                    r += 1                result[0] += right-r+1            tmp = []            l, r = left, mid+1            while l < mid+1 or r < right+1:                if r >= right+1 or (l < mid+1 and nums[l] <= nums[r]):                    tmp.append(nums[l])                    l += 1                else:                    tmp.append(nums[r])                    r += 1            nums[left:right+1] = tmp         nums = [x-y for x, y in itertools.izip(nums1, nums2)]        result = [0]        merge_sort(nums, 0, len(nums)-1, result)        return result[0] 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗