题目如下:

解题思路:题目要求的是对于任意一个区间i,要找出一个区间j,使得j的起点最接近i的终点。既然这样,我们可以把所有区间的终点组成一个列表,并按大小排序,使用二分查找就可以快速找到j区间。注意要保存新的列表和输入的区间列表的元素映射关系,这样才能快速找到j区间在输入区间列表的索引。

代码如下:

class Solution(object):
def findRightInterval(self, intervals):
"""
:type intervals: List[Interval]
:rtype: List[int]
"""
#print intervals[8].start,intervals[8].end
#print intervals[15].start,intervals[15].end
sl = intervals[:]
def cmpf(i1,i2):
if i1.start != i2.start:
return i1.start - i2.start
return i1.end - i2.end
sl.sort(cmp = cmpf) dic_inx = {}
for i,v in enumerate(intervals):
dic_inx[(v.start,v.end)] = i binL = []
for i, v in enumerate(sl):
binL.append(v.start)
res = []
for i in intervals:
import bisect
inx = bisect.bisect_left(binL,i.end)
if inx <= 0 or inx > len(binL) - 1:
res.append(-1)
else:
#res.append(inx)
res.append(dic_inx[(sl[inx].start, sl[inx].end)])
return res

【leetcode】436. Find Right Interval的更多相关文章

  1. 【LeetCode】436. Find Right Interval 解题报告(Python)

    [LeetCode]436. Find Right Interval 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  2. 【leetcode】1124. Longest Well-Performing Interval

    题目如下: We are given hours, a list of the number of hours worked per day for a given employee. A day i ...

  3. 【LeetCode】数组--合并区间(56)

    写在前面   老粉丝可能知道现阶段的LeetCode刷题将按照某一个特定的专题进行,之前的[贪心算法]已经结束,虽然只有三个题却包含了简单,中等,困难这三个维度,今天介绍的是第二个专题[数组] 数组( ...

  4. 【LeetCode】731. My Calendar II 解题报告(Python)

    [LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

  5. 【LeetCode】732. My Calendar III解题报告

    [LeetCode]732. My Calendar III解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar ...

  6. 【LeetCode】729. My Calendar I 解题报告

    [LeetCode]729. My Calendar I 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar- ...

  7. 【LeetCode】435. Non-overlapping Intervals 解题报告(Python)

    [LeetCode]435. Non-overlapping Intervals 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...

  8. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  9. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

随机推荐

  1. Step1 - How to: Define a Windows Communication Foundation Service Contract

    https://msdn.microsoft.com/en-us/library/ms731835.aspx This is the first of six tasks required to cr ...

  2. vs2017或vs2019添加引用时报错

    我先安装的是vs2019,进入VS命令提示符里后一直说:gacutil 不是有效的命令,一直没能解决,然后直接装了vs2017后,该命令可以使用了, 还是用VS2017吧,2019的版本感觉还有点问题 ...

  3. linux文件夹 权限为所有用户可 读写

    使用命令: sudo chmod dirname -R

  4. Spring MVC浅析

    讲到MVC,想必大家都很熟悉,就是将数据模型.视图.控制器进行分离,做到分工明确,在Spring的帮助下,Spring MVC 更是做到了充分的解耦,因为大部分的资源都由Spring进行管理,为Spr ...

  5. 关于在eclipse中配置tomcat的各种坑

    先说在windows下的,java环境什么的就不再记录了,记住装java ee之前,先要装好java se这样java ee才能顺利安装. 主要是安装好tomcat之后,在eclipse中进行配置的时 ...

  6. 巧用css内容生成

    1.      .box:before{content:"生成内容";}在.box内部的内容之前加上生成内容 2.       .box:after{content:"生 ...

  7. 【五一qbxt】day5 图论

    图论 学好图论的基础: 必须意识到图论hendanteng xuehuifangqi(雾 图 G = (V,E) 一般来说,图的存储难度主要在记录边的信息 无向图的存储中,只需要将一条无向边拆成两条即 ...

  8. kmp(多次无重叠匹配)

    http://acm.hdu.edu.cn/showproblem.php?pid=2087 剪花布条 Problem Description 一块花布条,里面有些图案,另有一块直接可用的小饰条,里面 ...

  9. python学习第十五天集合的创建和基本操作方法

    集合是python独有的数据列表,集合可以做数据分析,集合是一个无序的,唯一的的数据类型,可以确定列表的唯一性,说一下集合的创建和基本常见操作方法 1,集合的创建 s={1,2,4} 也可以用set( ...

  10. angularjs 信息链接 转摘自:http://www.zhihu.com/question/27427447

    这个问题嘛,真不好回答,问的太笼统了,其实你只要熟悉掌握了Angular.js,自然而然的就会用Angular.js结合自身的业务去构建SPA程序了,Angular.js是一个比较全面的框架,按照他的 ...