LeetCode 436. Find Right Interval

简介: Given a set of intervals, for each of the interval i, check if there exists an interval j whose start point is bigger than or equal to the end point of the interval i, which can be called that j is on the "right" of i.

v2-70acdc9456d65fa2fd074f2ba8e70428_1440w.jpg

Description



Given a set of intervals, for each of the interval i, check if there exists an interval j whose start point is bigger than or equal to the end point of the interval i, which can be called that j is on the "right" of i.


For any interval i, you need to store the minimum interval j's index, which means that the interval j has the minimum start point to build the "right" relationship for interval i. If the interval j doesn't exist, store -1 for the interval i. Finally, you need output the stored value of each interval as an array.


Note:

You may assume the interval's end point is always bigger than its start point.

You may assume none of these intervals have the same start point.


Example 1:


Input: [ [1,2] ]
Output: [-1]
Explanation: There is only one interval in the collection, so it outputs -1.


Example 2:


Input: [ [3,4], [2,3], [1,2] ]
Output: [-1, 0, 1]
Explanation: There is no satisfied "right" interval for [3,4].
For [2,3], the interval [3,4] has minimum-"right" start point;
For [1,2], the interval [2,3] has minimum-"right" start point.


Example 3:


Input: [ [1,4], [2,3], [3,4] ]
Output: [-1, 2, -1]
Explanation: There is no satisfied "right" interval for [1,4] and [3,4].
For [2,3], the interval [3,4] has minimum-"right" start point.


思路



  • 所有的区间的结尾不重复,因此构造一个字典,健为区间的开始位置,值为区间在原数组中的索引。
  • 将所有的区间的开始位置取出来形成一个数组 starts ,对数组按照从小到大排序。
  • 对于一个区间,记此区间结尾为 end,查找在 starts 数组中第一个大于等于 end 的数所在的位置 t,t 即为满足条件的区间。
  • 根据 t ,找到 t 在字典中对应的值即可确定区间的位置;对所有的区间都进行此操作。


# -*- coding: utf-8 -*-
# @Author:             何睿
# @Create Date:        2020-04-11 16:08:04
# @Last Modified by:   何睿
# @Last Modified time: 2020-04-11 16:34:06
from typing import List
class Solution:
    def findRightInterval(self, intervals: List[List[int]]) -> List[int]:
        starts = []
        index_dict = {}
        for index, interval in enumerate(intervals):
            starts.append(interval[0])
            index_dict[interval[0]] = index
        starts.sort()
        return list(self._binary_find(starts, interval[1], index_dict) for interval in intervals)
    def _binary_find(self, nums, target, index_dict):
        if target in index_dict:
            return index_dict[target]
        left, right = 0, len(nums) - 1
        middle = left + (right - left) // 2
        while left <= right:
            if nums[middle] >= target and (middle == 0 or nums[middle - 1] < target):
                return index_dict[nums[middle]]
            elif nums[middle] < target:
                left = middle + 1
            elif nums[middle] >= target and (middle == 0 or nums[middle - 1] >= target):
                right = middle - 1
            middle = left + (right - left) // 2
        return -1

源代码文件在 这里


目录
相关文章
|
11月前
|
Java
Leetcode 295. Find Median from Data Stream
在一个有序数组中找中位数,但需要支持再数组中添加新的元素。本来是有序里的,可以很轻易就查到中位数,但如果添加新数字后,不一定有序。如果先对数组排序,那代价就比较大了,每次排序时间复杂度O(n*log(n)),看discuss发现了一种很巧妙的解法,可以把添加数据的时间复杂度降低到O(log(n)) ,查询中位数O(1)。
45 0
|
11月前
Leetcode Find Minimum in Rotated Sorted Array 题解
对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数,注意,K有可能是0,也就是没有翻转。
42 0
|
算法 Python
LeetCode 1160. 拼写单词 Find Words That Can Be Formed by Characters
LeetCode 1160. 拼写单词 Find Words That Can Be Formed by Characters
LeetCode 1160. 拼写单词 Find Words That Can Be Formed by Characters
LeetCode 389. Find the Difference
给定两个字符串 s 和 t,它们只包含小写字母。 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。 请找出在 t 中被添加的字母。
111 0
LeetCode 389. Find the Difference
|
索引
LeetCode 373. Find K Pairs with Smallest Sums
给定两个以升序排列的整形数组 nums1 和 nums2, 以及一个整数 k。 定义一对值 (u,v),其中第一个元素来自 nums1,第二个元素来自 nums2。 找到和最小的 k 对数字 (u1,v1), (u2,v2) ... (uk,vk)。
146 0
LeetCode 373. Find K Pairs with Smallest Sums
|
算法 Python
LeetCode 295. Find Median from Data Stream
中位数是有序列表中间的数。如果列表长度是偶数,中位数则是中间两个数的平均值。
92 0
LeetCode 295. Find Median from Data Stream
|
索引
LeetCode 287. Find the Duplicate Number
给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数。假设只有一个重复的整数,找出这个重复的数。
83 0
LeetCode 287. Find the Duplicate Number
|
索引
LeetCode 162. Find Peak Element
给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1],找到峰值元素并返回其索引。 数组可能包含多个峰值,在这种情况下,返回任何一个峰值所在位置即可。
94 0
LeetCode 162. Find Peak Element
|
算法
LeetCode Find Minimum in Rotated Sorted Array II
假设按照升序排序的数组在预先未知的某个点上进行了旋转。 ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。 请找出其中最小的元素。 注意数组中可能存在重复的元素。
80 0
LeetCode Find Minimum in Rotated Sorted Array II
LeetCode 153. Find Minimum in Rotated Sorted Array
假设按照升序排序的数组在预先未知的某个点上进行了旋转。 ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。 请找出其中最小的元素。 你可以假设数组中不存在重复元素。
100 0
LeetCode 153. Find Minimum in Rotated Sorted Array