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

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

  1. [LeetCode] Binary Search 二分搜索法

    Given a sorted (in ascending order) integer array nums of n elements and a target value, write a fun ...

  2. LeetCode Binary Search All In One

    LeetCode Binary Search All In One Binary Search 二分查找算法 https://leetcode-cn.com/problems/binary-searc ...

  3. LeetCode & Binary Search 解题模版

    LeetCode & Binary Search 解题模版 In computer science, binary search, also known as half-interval se ...

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

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  5. LeetCode Binary Search Tree Iterator

    原题链接在这里:https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a bina ...

  6. [Leetcode] Binary search -- 475. Heaters

    Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...

  7. 153. Find Minimum in Rotated Sorted Array(leetcode, binary search)

    https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/ leetcode 的题目,binary ...

  8. [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 follo ...

  9. [Leetcode] Binary search, DP--300. Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  10. LeetCode: Binary Search Tree Iterator 解题报告

    Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...

随机推荐

  1. tomcat的环境搭建

    tomcat搭建过程还是比较简单的,只需要安装好jdk,然后配置好环境变量,最后把tomcat安装上开启就可以了. 首先下载jdk,然后把下载下来的jdk放到/usr/local下,然后用rpm -i ...

  2. PHP 学习笔记(3)

    <?phpif (isset($_POST['action']) && $_POST['action'] == 'submitted') {    echo '<pre&g ...

  3. 基于vue2.0前端组件库element中 el-form表单 自定义验证填坑

    eleme写的基于vue2.0的前端组件库: http://element.eleme.io 我在平时使用过程中,遇到的问题. 自定义表单验证出坑: 1: validate/resetFields 未 ...

  4. 蓝桥杯-组素数-java

    /* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2016, 广州科技贸易职业学院信息工程系学生 * All rights reserved. * 文件名称: ...

  5. 【MySql】——MHA+GTID+failover+binlog-server+Atlas

    一.环境准备 1.mysql-db01 #系统版本 [root@mysql-db01 ~]# cat /etc/redhat-release CentOS release 6.7 (Final) #内 ...

  6. Python进阶之装饰器

    函数也是对象 要理解Python装饰器,首先要明白在Python中,函数也是一种对象,因此可以把定义函数时的函数名看作是函数对象的一个引用.既然是引用,因此可以将函数赋值给一个变量,也可以把函数作为一 ...

  7. linux下MongoDB客户端shell基本操作

    MongoDB 是一款NoSql数据库,没有固定的模式,即同一个集合中的不同文档结构可以不同,如:第一条记录{name:”xiaoming”},第二条记录:{name:”xiaoli”,age:15} ...

  8. DDD领域驱动 (一)

    说道DDD不得不说传统的架构与DDD的架构区别. 传统的架构不外乎就是三层,而在这三层里面又不断的细分,始终没有达到想要的效果,那么为什么当时还是采用三层. 当然在DDD没有提出的时候三层是大多数人的 ...

  9. Struts2中的JSON问题——后台返回JSON字符串到前台

    最近做一个项目遇到一个比较棘手的问题,项目后台采用struts2+Hibernate3+Spring3,前台采用ExtJs4.笔者目前仍是一名大二学生吗,后台框架完全是毫无任何基础,从零学,现学现用. ...

  10. Spring-web中的web.xml为Servlet提供的配置选项说明

    配置Servlet时可以设置的一些初始化参数,总结如下: ContextAttribute: 在ServletContext的属性中,要用作WebApplicationContext的属性名称. Co ...