[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 ...
随机推荐
- C++ 头文件系列(system_error)
1.为什么system_error "....report error conditions originating from the operating system or low-lev ...
- html基础认识,高手别看
HTML5是一种用于在万维网上构建和呈现内容的符号言语.它是HTML规范的第五和当时版别.它是由万维网联盟(W3C)在十月发布的2014 [ 2 ] [ 4 ]和最新的多媒体支持进步言语,一起坚持它简 ...
- 【源码分析】cJSON库学习
cJSON库是什么? cJSON是一个轻量级的json解析库.使用起来非常简单,整个库非常地简洁,核心功能的实现都在cJSON.c文件,非常适合阅读源代码来学习C语言.最近读完这个库的源码,分享自己收 ...
- 分布式文件系统:HDFS
学习Hadoop,两个东西肯定是绕不过,MapReduce和HDFS,上一篇博客介绍了MapReduce的处理流程,这一篇博客就来学习一下HDFS. HDFS是一个分布式的文件系统,就是将多台机器的存 ...
- lighttpd启动问题
/home/yuna/web/app/lighttpd/sbin/lighttpd -f /home/yuna/web/app/lighttpd/lighttpd.conf -m /home/yuna ...
- 关于v-model、v-for、v-on的用法
展示Holle Vue window.onload = function(){ var box = new Vue({ el:'#div', ...
- shapeless官方指南翻译总结
今天抽空把之前翻译的<The Type Astronaut's Guide to Shapeless>一书放到了Gitbook上,将其开源供所有人阅读并希望大家能够提出宝贵意见,地址为sh ...
- mysql5.7.16二进制安装
1.下载二进制文件 cd /data wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.16-linux-glibc2.5-x ...
- Visual Studio for Mac 初体验
你喜爱的 IDE,现在可用于 Mac 来自:https://www.visualstudio.com/zh-hans/vs/visual-studio-mac/ 惊不惊喜?意不意外?惊喜但不意外,因为 ...
- zen coding一个牛的不行的html和css开发工具
zen coding 是一种仿css选择器的语法来快速开发html和css的开源项目.现已更名为Emmet.可以到github上下载拜读.在这个都想偷懒的世界里,此方法可以极大的缩短开发人员的开发时间 ...