[Leetcode] Binary search--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.

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:

  1. You may assume the interval‘s end point is always bigger than its start point.
  2. You may assume none of these intervals have the same start point.

Solution:

1.  1st thing I come up with is to use binary search, but it has LTE problem

(1) maintain the index of each interval by create a tuple

(2) sort the list by the start point

(3) iterate each interval, find the end point endElement

(4) binary search the right most insertion position of each endElement with each rest interval start point, and then find the index

        def binarySearchStartPoint(targetLst, ele):
            if len(targetLst) == 1:            #only one element
                if targetLst[0][0].start >= ele:
                    return targetLst[0][1]
                else:
                    return -1
            l = 0
            h = len(targetLst)-1
            while (l <= h):
                mid = (l + h)/2
                if ele == targetLst[mid][0].start:
                    return targetLst[mid][1]                    #index
                elif ele < targetLst[mid][0].start:
                    h = mid - 1
                else:
                    l = mid + 1
            #print ( ‘ddd : ‘, len(targetLst), l)
            if l >= len(targetLst):
                return -1
            return targetLst[l][1]

        intersIndexLst = [(intl, i) for i, intl in enumerate(intervals)]

        intersIndexSortedLst = sorted(intersIndexLst, key = lambda k: k[0].start)
        i = 0
        ansLst = [0] * len(intersIndexSortedLst)

        while (i < len(intersIndexSortedLst)-1):
            endElement = intersIndexSortedLst[i][0].end
            targetLst = intersIndexSortedLst[i+1:]
            currInd = intersIndexSortedLst[i][1]
            indRight = binarySearchStartPoint(targetLst, endElement)
            #print (‘targetLst: ‘, endElement, indRight)
            ansLst[currInd] = indRight
            i += 1
        ansLst[intersIndexSortedLst[-1][1]] = -1          #the last emelement in intersIndexSortedLst
        return ansLst

2.

I refer to other‘s solution, which makes the question so simple.
(1) only need to maintain the start point with a tuple
(2) bisect can be used in that way,
(3) after sorted, directly compare the end with the original interval list to find the insertion position (index)

       ansLst = []
        intersIndexLst = [(intl.start, i) for i, intl in enumerate(intervals)]
        intersIndexSortedLst = sorted(intersIndexLst)
        for intl in intervals:
            ind = bisect_left(intersIndexSortedLst, (intl.end, ))
            ansLst.append(intersIndexSortedLst[ind][1] if ind < len(intervals) else - 1)
        return ansLst

--reference:

https://discuss.leetcode.com/topic/65596/python-o-nlogn-short-solution-with-explanation

时间: 2024-10-13 15:16:14

[Leetcode] Binary search--436. Find Right Interval的相关文章

[LeetCode]Binary Search Tree Iterator,解题报告

题目 LeetCode题目如下: mplement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: next() and hasNext() should run in average O(1

[LeetCode] Binary Search Tree Iterator

Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: next() and hasNext() should run in

[Leetcode] Binary search/tree-230. Kth Smallest Element in a BST

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ? k ? BST's total elements. Follow up:What if the BST is modified (insert/delete operations) often and you need

LeetCode——Binary Search Tree Iterator

Description: Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: next() and hasNext() should run in average O(1) t

[leetcode] Binary Search Tree

题目:(Tree Stack) Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: next() and hasNext() should run in average O(1

[Leetcode] Binary search -- 222. Count Complete Tree Nodes

Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from Wikipedia:In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as pos

[Leetcode] Binary search, Divide and conquer--240. Search a 2D Matrix II

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascending from left to right. Integers in each column are sorted in ascending from top to bottom.

Leetcode Binary Search Conclusion

278 First Bad Version23.7%Easy 278. First Bad Version 349 Intersection of Two Arrays44.5%Easy 374 Guess Number Higher or Lower32.2%Easy 350 Intersection of Two Arrays II42.6%Easy 270 Closest Binary Search Tree Value 36.3%Easy 50 Pow(x, n)27.4%Medium

[LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: next() and hasNext() should run in average O(1) time and uses

LeetCode: Binary Search Tree Iterator 解题报告

Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: next() and hasNext() should run in