from functools import reduce


def combine_int_overlaps(ranges):
    """
    Merge the range pairs if there is overlap exists between the given ranges.
    This algorithm needs a sorted range list in terms of the start time.
    Note that neighboring numbers lead to a merged range.
    Example:
        [(1, 10), (11, 20)] -> [(1, 20)]

    Refer to the original code at https://stackoverflow.com/a/59378428

    Args:
        ranges(list):
            List containing ranges.
            Example: [(102, 103), (104, 109), (107, 120)]
    Returns:
        merged_list (list):
            List containing the combined ranges.
            Example: [(102, 120)]

    """
    ranges = sorted(ranges, key=lambda x: x[0])
    merged_list = reduce(
        lambda x, element: x[:-1:] + [(min(*x[-1], *element), max(*x[-1], *element))]
        if x[-1][1] >= element[0] - 1
        else x + [element],
        ranges[1::],
        ranges[0:1],
    )
    return merged_list