[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:
- 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.
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的更多相关文章
- [LeetCode] Binary Search 二分搜索法
Given a sorted (in ascending order) integer array nums of n elements and a target value, write a fun ...
- LeetCode Binary Search All In One
LeetCode Binary Search All In One Binary Search 二分查找算法 https://leetcode-cn.com/problems/binary-searc ...
- LeetCode & Binary Search 解题模版
LeetCode & Binary Search 解题模版 In computer science, binary search, also known as half-interval se ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- LeetCode Binary Search Tree Iterator
原题链接在这里:https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a bina ...
- [Leetcode] Binary search -- 475. Heaters
Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...
- 153. Find Minimum in Rotated Sorted Array(leetcode, binary search)
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/ leetcode 的题目,binary ...
- [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 ...
- [Leetcode] Binary search, DP--300. Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- LeetCode: Binary Search Tree Iterator 解题报告
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
随机推荐
- Kruskal和Prim算法求最小生成树
Kruskal算法求最小生成树 测试数据: 5 6 0 1 5 0 2 3 1 2 4 2 4 2 2 3 1 1 4 1 输出: 2 3 1 1 4 1 2 4 2 0 2 3 思路:在保证不产生回 ...
- MySQL 的Coalesce函数
今天用到了coalesce 函数,原因在于,我想要查找合同到期日的字段是否有值(因为合同到期日分3个字段,对应着不同的日期) select coalesce(contract_date1,contra ...
- day01课程回顾,数据类型
Day01 Python的分类 Cpython:代码àc字节码->机器码 一行一行的编译执行 Pypy: 代码àc字节码->机器码 全部转换完再执行 其他python 代码- ...
- java.net.SocketException: Broken pipe 异常可能的原因
org.apache.catalina.connector.ClientAbortException: java.net.SocketException: Broken pipe at org.apa ...
- nginx与apache配合反向代理技术2
注意,上次我们只是简单的在同一台服务器模拟搭建了一个新的http服务器(启用了8080端口),使用的是apache,从而模拟了多台服务器实现的Nginx反向代理,通过Nginx向上游代理服务器发送请求 ...
- 蓝桥杯-密码发生器-java
/* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2016, 广州科技贸易职业学院信息工程系学生 * All rights reserved. * 文件名称: ...
- String属于“假引用类型”,代码为证(一个String引发的血案...)
一直以为String是引用类型,今天写了个浅拷贝的测试,发现String有基本类型的特征. class A{ public int a = 555; } class User implements C ...
- map中结构体做关键字的注意事项
序: 今天做一道题,由于递归函数比较恶心,如果用记忆化搜索,数据范围极大却又用不全(二维数组存的话直接炸).所以决定干脆使用stl::map存储(反正有O2优化),但是执行insert的时候,编译器却 ...
- 使用java API操作hdfs--读取hdfs文件并打印
在myclass之中创建类文件,这个myclass目录是自己创建的. 编译的时候会报如下的错误: 很明显就是没有导入包的结果 见这个API网站,则可以找到响应的包,当然还有java的api文档 htt ...
- Collection学习目录
1.Collection<E>.Iterable<T>和Iterator<E>接口 2.ArrayList源码分析 3.LinkedList源码解析 4.Vecto ...