[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 ...
随机推荐
- UPS对电源故障的处理能力
UPS对电源故障的处理能力 双变换在线式UPS由于其逆变器实时在线工作,因而能对所有的电源故障具有隔离和处理功能.由于目前电网情况发生了很大变化,真正的长时间断电只占所有电源故障的30%甚至更低,而非 ...
- 一个全局变量引起的DLL崩溃
参考我发的帖子: http://bbs.csdn.net/topics/390737064?page=1#post-397000946 现象是exe程序在加载dll的时候崩溃了,莫名其妙的崩溃了.换其 ...
- CF #312 E. A Simple Task 线段树
题目链接:http://codeforces.com/problemset/problem/558/E 给一个字符串,每次对一个区间内的子串进行升序或者降序的排列,问最后字符串什么样子. 对于字符串排 ...
- Android -- 贝塞尔实现水波纹动画(划重点!!)
1,昨天看到了一个挺好的ui效果,是使用贝塞尔曲线实现的,就和大家来分享分享,还有,在写博客的时候我经常会把自己在做某种效果时的一些问题给写出来,而不是像很多文章直接就给出了解决方法,这里给大家解释一 ...
- js继承与闭包(笔记)
1.一切引用类型都是对象,对象时属性的集合:typeof null === 'object'(例外): 2.对象都是通过函数创建来的,比如var obj = new Object();typeof O ...
- js函数中this的指向
本文是我个人对this指向的一些理解,如有不足之处,还望大家可以批评指正,在此先谢过了! 首先,我们来回顾一下ES5里函数的几种调用方式: 1⃣️直接调用 foo(); 2⃣️方法调用 obj.foo ...
- IntelliJ-项目配置,解决no artifacts的warnings
1.名词解释 artifacts:是maven中的一个概念,表示项目/modules如何打包,比如jar,war,war exploded,ear等打包形式, 一个项目或者说module有了artif ...
- BogoMIPS与calibrate_delay
在分析Arm+linux启动信息的时候.发现有一个信息竟然耗费了2s的时间,这简直是不能忍受的.这个耗时大鳄是什么东西哪,请看分析信息: [ 0.000000] console [ttyMT0] ...
- 【算法系列学习】HDU 5527 Too Rich贪心
http://www.cnblogs.com/AOQNRMGYXLMV/p/4934747.html #include<iostream> #include<cstdio> # ...
- 配置web.xml和glassfish容器实现javaEE表单验证
web.xml配置: <!-- 声明用于安全约束的角色 --> <security-role> <role-name>ReimUser</role-name& ...