最小区间使得至少包含每组元素中的一个
问题描述:
You have k lists of sorted integers. Find the smallest range that includes at least one number from each of the k lists.
For example,
List 1: [4, 10, 15, 24, 26]
List 2: [0, 9, 12, 20]
List 3: [5, 18, 22, 30]
The smallest range here would be [20, 24] as it contains 24 from list 1, 20 from list 2, and 22 from list 3.
问题解答:
建立大小为k的最小堆,堆的每个元素都来自不同的组。统计堆中的最大元素已经最小元素,计算区间跨度值。每次删除堆顶元素,然后将该元素所在的那个组下一个元素加入堆中。再次计算跨度。循环直到有一个组没有元素可以加入堆了,即为空。
算法很巧妙,正确性也是很容易证明滴,哈哈,该睡觉了。晚安