【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 ...
随机推荐
- mysql优化方法陈列
高并发大多的瓶颈在后台,在存储,mysql的正常的优化方案如下: 1)代码中sql语句优化 2)数据库字段优化,索引优化 3)加缓存,redis/memcache等 4)主从,读写分离 5)分区表 6 ...
- web性能测试工具——http_load
http_load是一款基于Linux平台的web服务器性能测试工具,用于测试web服务器的吞吐量与负载,web页面的性能. http_load是基于linux.unix平台的一种性能测工具 它以并行 ...
- 避免UE4项目VS中误改源码.h文件导致编译时间长
最近几天两次触发VS中误改UE4源码头文件,导致需要编译大量源码的情况:再好的习惯也有不可靠的时候,还是需要可靠方案解决这个问题:官方提供了预编译版本(即从Launcher中下载的版本),但是对于程序 ...
- 动态生成多个选择项【c#】
<asp:CheckBoxList ID="cbxLabelList" runat="server" RepeatColumns="10&quo ...
- 学习java的第二十二天
一.今日收获 1.java完全学习手册第三章算法的3.2排序,比较了跟c语言排序上的不同 2.观看哔哩哔哩上的教学视频 二.今日问题 1.快速排序法的运行调试多次 2.哔哩哔哩教学视频的一些术语不太理 ...
- tomcat源码1
Lifecycle:(接口) LifecycleBase:abstract:添加,删除Listener,各种init,start,stop,destory LifecycleMBeanBase:abs ...
- d3 CSS
CSS的inline.block与inline-block 块级元素(block):独占一行,对宽高的属性值生效:如果不给宽度,块级元素就默认为浏览器的宽度,即就是100%宽. 行内元素(inline ...
- Output of C++ Program | Set 13
Predict the output of following C++ program. 1 #include<iostream> 2 using namespace std; 3 4 c ...
- 在调用系统相册时,UIIMagePickerController使用中偷换StatusBar颜色的问题
在调用系统相册时,UIIMagePickerController使用中偷换StatusBar颜色的问题 此时解决办法是 #pragma mark - UIImagePickerController D ...
- Handler与多线程
1.Handler介绍 在Android开发中,我们常会使用单独的线程来完成某些操作,比如用一个线程来完成从网络上下的图片,然后显示在一个ImageView上,在多线程操作时,Android中必须保证 ...