题目如下:

解题思路:题目要求的是对于任意一个区间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. 170821-关于SpringMVC的知识点

    1.SpringMVC 概述以及优势 SpringMVC和Spring的关系:  软件开发的三层架构: web层[表示层.表现层]---->Service层---->Dao[DataBas ...

  2. face_recognition开源人脸识别库:离线识别率高达99.38%

    基于Python的开源人脸识别库:离线识别率高达99.38%——新开源的用了一下感受一下 原创 2017年07月28日 21:25:28 标签: 人脸识别 / 人脸自动定位 / 人脸识别开源库 / f ...

  3. win10下VMware15运行ubuntu18.04无法和主机之间复制粘贴问题

    可以运行以下命令行: sudo apt-get autoremove open-vm-tools sudo apt-get install open-vm-tools sudo apt-get ins ...

  4. SPOJ AEROLITE

    题目链接: http://www.spoj.com/problems/AEROLITE/en/ ---------------------------------------------------- ...

  5. php中的构造函数与析构函数

    PHP面向对象——构造函数.析构函数 __construct.__destruct__construct 构造方法,当一个对象创建时调用此方法,使用此方法的好处是:可以使构造方法有一个独一无二的名称, ...

  6. Shell中uname命令查看系统内核、版本

    uname命令 描述 用于打印内核名称和版本.主机名等系统信息. 用法 uname [OPTION]... 参数     用法 -a print all information -s print th ...

  7. 洛谷T89644 palindrome回文串

    洛谷 T89643 回文串(并查集) 洛谷:https://www.luogu.org/problem/T89643 题目描述 由于 Kiana 实在是太忙了,所以今天的题里面没有 Kiana. 有一 ...

  8. debian7下安装eclipse

    apt-get install build-essential 完成后从eclipse官网上下载C++专用的版本,直接解压缩即可

  9. Codeforces Round #285 (Div. 2)C. Misha and Forest(拓扑排序)

    传送门 Description Let's define a forest as a non-directed acyclic graph (also without loops and parall ...

  10. dpkg:处理软件包 xxx (--configure)时出错

    9月 17 16:11:35 xiakaibi-PC systemd[1]: Starting LSB: Start Jenkins at boot time...9月 17 16:11:35 xia ...