作者: 负雪明烛
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:

  1. The given list may contain duplicates, so ascending order means >= here.
  2. 1 <= k <= 3500
  3. -105 <= value of elements <= 105.
  4. 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)的更多相关文章

  1. [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 ...

  2. [leetcode]632. Smallest Range最小范围

    You have k lists of sorted integers in ascending order. Find the smallest range that includes at lea ...

  3. 【LeetCode】373. Find K Pairs with Smallest Sums 解题报告(Python)

    [LeetCode]373. Find K Pairs with Smallest Sums 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/p ...

  4. 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)

    [LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...

  5. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  6. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  7. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  8. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  9. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

随机推荐

  1. Go语言缺陷

    我为什么放弃Go语言 目录(?)[+] 我为什么放弃Go语言 有好几次,当我想起来的时候,总是会问自己:我为什么要放弃Go语言?这个决定是正确的吗?是明智和理性的吗?其实我一直在认真思考这个问题. 开 ...

  2. Maven打包及场景

    场景一 对当前项目打包并指定主类. <build> <plugins> <plugin> <artifactId>maven-compiler-plug ...

  3. 【leetcode】43. Multiply Strings(大数相乘)

    Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...

  4. Java实现单链表的增删查改及逆置打印

    //所提供的接口 LinkList.java package Struct; public interface LinkList {//判断链表为空public boolean linkListIsE ...

  5. Android Loader异步装载

    一.Loader简介: (一).Loader的概念: 装载器从android3.0开始引进.它使得在activity或fragment中异步加载数据变得简单. 当成批显示数据的时候,为了使用户体验更好 ...

  6. canal整合springboot实现mysql数据实时同步到redis

    业务场景: 项目里需要频繁的查询mysql导致mysql的压力太大,此时考虑从内存型数据库redis里查询,但是管理平台里会较为频繁的修改增加mysql里的数据 问题来了: 如何才能保证mysql的数 ...

  7. 【编程思想】【设计模式】【行为模式Behavioral】访问者模式Visitor

    Python版 https://github.com/faif/python-patterns/blob/master/behavioral/visitor.py #!/usr/bin/env pyt ...

  8. Spring MVC入门(一)—— RestTemplate组件

    ClientHttpRequestFactory 它是个函数式接口,用于根据URI和HttpMethod创建出一个ClientHttpRequest来发送请求~ ClientHttpRequest它代 ...

  9. python自带性能强悍的标准库 itertools

    可迭代对象就像密闭容器里的水,有货倒不出 itertools是python内置的标准模块,提供了很多简洁又高效的专用功能,使用得当能够极大的简化代码行数,同时所有方法都是实现了生成器函数,这就意味着极 ...

  10. Mysql资料 主键

    目录 一.简介 二.操作 三.技巧 一.简介 主键意味着表中每一行都应该有可以唯一标识自己的一列(或一组列). 一个顾客可以使用顾客编号列,而订单可以使用订单ID,雇员可以使用雇员ID 或 雇员社会保 ...