algorithm - kth largest element in range interval
问题描述
Given a list of overlapping intervals of integers. I need to find the kth largest element.
Example:
List { (3,4), (2,8), (4,8), (1,3), (7,9) }
This interval represents numbers as
[3, 4], [2, 3, 4, 5, 6, 7, 8], [4, 5, 6, 7, 8], [1, 2, 3], and [7, 8, 9].
If we merge and sort it in decreasing order, we get
9, 8, 8, 8, 7, 7, 7, 6, 6, 5, 5, 4, 4, 4, 3, 3, 3, 2, 2, 1
Now the 4th largest number in the list is 8.
Can anyone please explain an efficient (we don't have to generate the list) algorithm to find the kth element given only a list of internals ?
解决方案
- 找出最大的数。您遍历间隔并检查间隔的结束。在您的情况下,它是 9. Set
k = 1
和L = 9
。 - 也许还有其他 9。将区间标记
(7,9)
为已访问并检查是否有任何其他区间包含 9a >= 9 && b <= '
。在您的情况下,只有一个 9。 - 减少当前最大数字 (
L -= L
) 并清除访问间隔的历史记录。并重复检查间隔。 - 每次你在一个区间内遇到你当前的最大数字时,你应该增加
k
这个区间并将其标记为已访问。一旦它等于kth
当前的最大数字L
就是你的答案。
推荐阅读
- mongodb - 如何找出为什么 MongoDB 会消耗这么多内存?
- java - 我的循环没有执行特定的迭代
- php - 从多键多维数组中提取单个索引值(从每个子数组中)
- gnuplot - 如何在gnuplot中应用tail命令
- java - 在 Java JDBC 中使用动态 where 子句
- java - Spring bean 在控制器类中返回 NULL
- asp.net-core - NSwag 和多个 api 版本
- javascript - D3 图形工具提示应在悬停时显示最近点
- spring-boot - 当属性位于 Spring-boot 文件 (application.properties) 而不是 liquibase.properties 时,从命令行执行 liquibase:rollback
- python - 如何设置 cvxpy n-dim 变量的第一个值?