【LeetCode】632. Smallest Range 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址: https://leetcode.com/problems/smallest-range/description/
题目描述:
You have k lists of sorted integers in ascending order. Find the smallest range that includes at least one number from each of the k lists.
We define the range [a,b] is smaller than range [c,d] if b-a < d-c or a < c if b-a == d-c.
Example 1:
Input:[[4,10,15,24,26], [0,9,12,20], [5,18,22,30]]
Output: [20,24]
Explanation:
List 1: [4, 10, 15, 24,26], 24 is in range [20,24].
List 2: [0, 9, 12, 20], 20 is in range [20,24].
List 3: [5, 18, 22, 30], 22 is in range [20,24].
Note:
- The given list may contain duplicates, so ascending order means >= here.
- 1 <= k <= 3500
- -105 <= value of elements <= 105.
- For Java users, please note that the input type has been changed to List<List>. And after you reset the code template, you’ll see this point.
题目大意
找出一个最小的区间,这个区间对每个数组都至少包含其中的一个数字。
解题方法
这个题是76. Minimum Window Substring的变形,第76题要我们查找出s中一个最小的窗口,使得这个窗口中包含t的所有字符。如果把本题的nums中的每个数组合并成一个总的数组,那么就是找出一个小的窗口,使得这个窗口包含有不同区间的最少一个字符。
所以把nums放到一个数组里面去,放的过程中需要把这个数组的索引号也放进去。然后就可以通过查找出一个小的区间,这个区间里包含所有数组的索引号了。就是第76题。
使用right指针向右搜索,同时要记录在left~right这个区间内包含的数组个数和。如果在[left,right]区间内,数组个数和的个数和与原始数组个数相等了,说明在这个区间是符合要求的一个区间,但是不一定是最短区间。
因此,现在要移动left指针,要求,在[left, right]区间内的数组出现个数应该把所有的数组个数都进行包含。同样使用cnt来验证是否包含了所有的数组。
在移动left指针的时候要注意存储最短的区间,当所有的循环都结束之后最短区间即为题目要求了。
这个题使用字典保存不同数组出现的次数,以此来维护cnt。
这个题是寻找子字符串的模板题,应该记住。
时间复杂度是O(N*log(N) + N),空间复杂度是O(N)。其中N是所有数组的长度和。
class Solution(object):
def smallestRange(self, nums):
"""
:type nums: List[List[int]]
:rtype: List[int]
"""
v = list()
for i in range(len(nums)):
for num in nums[i]:
v.append((num, i))
v.sort()
l, r, n = 0, 0, len(v)
d = collections.defaultdict(int)
k = len(nums)
cnt = 0
res = [0, 0]
diff = float('inf')
while r < n:
if d[v[r][1]] == 0:
cnt += 1
d[v[r][1]] += 1
while l <= r and cnt == k:
if v[r][0] - v[l][0] < diff:
diff = v[r][0] - v[l][0]
res = [v[l][0], v[r][0]]
d[v[l][1]] -= 1
if d[v[l][1]] == 0:
del d[v[l][1]]
cnt -= 1
l += 1
r += 1
return res
参考资料:
http://www.cnblogs.com/grandyang/p/7200016.html
日期
2018 年 10 月 3 日 —— 玩游戏导致没睡好,脑子是浆糊。
【LeetCode】632. Smallest Range 解题报告(Python)的更多相关文章
- [LeetCode] 632. Smallest Range Covering Elements from K Lists 覆盖K个列表元素的最小区间
You have k lists of sorted integers in ascending order. Find the smallest range that includes at lea ...
- [leetcode]632. Smallest Range最小范围
You have k lists of sorted integers in ascending order. Find the smallest range that includes at lea ...
- 【LeetCode】373. Find K Pairs with Smallest Sums 解题报告(Python)
[LeetCode]373. Find K Pairs with Smallest Sums 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/p ...
- 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)
[LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
随机推荐
- android 点击图片从Fragment跳转到activity
android 点击图片从Fragment跳转到activity 在Fragment里编写 public View onCreateView(@NonNull LayoutInflater infla ...
- 基本绘图函数:plot的使用
注意:"##"后面是程序输出结果 例如: par("bg") # 命令 ## [1] "white" # 结果 基本绘图函数: plot:散 ...
- 32-3Sum
相似题目 4sum http://blog.csdn.net/justdoithai/article/details/51195124 http://blog.csdn.net/justdoithai ...
- EXCEL如何用公式提取一列中的唯一值和不重复值
说明:思路用的很新奇,也对COUNTIF有了更深一步的了解,但是,对于百行数据运算速度特别低,不适合数据多的使用 当面对一堆数据,我们要提取一列的唯一值的时候,如果单纯用人为一个个判断,显然是不科学的 ...
- Flume消费内外网分流配置的Kafka时遇到的坑
网上有铺天盖地的文章,介绍如何将Kafka同时配置成公网地址.内网地址,以实现内外网分流,看着都很成功. 但我们通过Flume消费一个配置了内外网分流的Kafka(版本0.10.1)集群时遇到了坑,却 ...
- SpringBoot整合Shiro 二:Shiro配置类
环境搭建见上篇:SpringBoot整合Shiro 一:搭建环境 Shiro配置类配置 shiro的配置主要集中在 ShiroFilterFactoryBean 中 关于权限: anon:无需认证就可 ...
- 论文翻译:2020_Weighted speech distortion losses for neural-network-based real-time speech enhancement
论文地址:基于神经网络的实时语音增强的加权语音失真损失 论文代码:https://github.com/GuillaumeVW/NSNet 引用:Xia Y, Braun S, Reddy C K A ...
- 日常Java 2021/10/28
Java lterator Java lterator(迭代器)不是一个集合,它是一种用于访问集合的方法,可用于迭代 ArrayList和HashSet等集合.lterator是Java迭代器最简单的 ...
- java中类实现Serializable接口的原因
背景:一个java中的类只有实现了Serializable接口,它的对象才是可序列化的.如果要序列化某些类的对象,这些类就必须实现Serializable接口.Serializable是一个空接口,没 ...
- Scala(五)【集合的高级使用】
目录 一.集合属性 size length contains mkString 二.集合方法 drop.dropRight distinct:去重 head.last:获取第一个.最后一个元素 tai ...