lc 632. Smallest Range

https://leetcode.com/problems/smallest-range/description/

给你k个数组,找一个最小区间[a,b],可以包含k个数组中的数字各至少一个。

滑动窗口题。

对于要求“最短”的题目很适用。

points:

1.在扩张右界的时候,一旦碰到合法就停止,但不用记录结果。在收缩左界的时候进行记录(判断)。

code:

import heapq
class Solution:
    def __init__(self):
        self.a=None
        self.b=None
    def smallestRange(self, nums):
        """
        :type nums: List[List[int]]
        :rtype: List[int]
        """

        def newAns(a,b):
            if self.a==None or b-a<self.b-self.a:
                self.a=a
                self.b=b

        k=len(nums)
        cnt=[0]*k
        n=0

        tem=[]
        for i in range(len(nums)):
            arr=nums[i]
            tem+=[(arr[j],i) for j in range(len(arr))]
        nums=tem
        nums.sort()

        i=0
        j=-1

        while j<len(nums):
            while j<len(nums) and n<k:
                j += 1
                if j==len(nums):
                    break
                cnt[nums[j][1]] += 1
                if cnt[nums[j][1]] == 1:
                    n += 1
            while i<len(nums) and n==k:
                newAns(nums[i][0], nums[j][0])
                cnt[nums[i][1]] -= 1
                if cnt[nums[i][1]] == 0:
                    n -= 1
                i += 1
                if i == len(nums):
                    break

        return [self.a,self.b]

原文地址:https://www.cnblogs.com/waldenlake/p/9639673.html

时间: 2024-08-29 05:27:04

lc 632. Smallest Range的相关文章

[LeetCode] 632. Smallest Range Covering Elements from K Lists

[LeetCode]632. Smallest Range Covering Elements from K Lists 你有 k 个升序排列的整数数组.找到一个最小区间,使得 k 个列表中的每个列表至少有一个数包含在其中. 我们定义如果 b-a < d-c 或者在 b-a == d-c 时 a < c,则区间 [a,b] 比 [c,d] 小. 示例 1: 输入:[[4,10,15,24,26], [0,9,12,20], [5,18,22,30]] 输出: [20,24] 解释: 列表 1:

一道题目- Find the smallest range that includes at least one number from each of the k lists

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]

[LeetCode] 910. Smallest Range II 最小区间之二

Given an array?A?of integers, for each integer?A[i]?we need to choose?either?x = -K?or?x = K, and add?x?to?A[i]?(only once). After this process, we have some array?B. Return the smallest possible difference between the maximum value of?B?and the mini

LeetCode 910. Smallest Range II

很有意思的一道数学推理题目, 剪枝以后解法也很简洁.初看貌似需要把每个数跟其他数作比较.但排序以后可以发现情况大大简化:对于任一对元素a[i] < a[j], a[i] - k和a[j] + k 的情况可以排除, 因为会产生比原值更大的差, 所以对于原有数组的最小值min最大值max, (min - k, max + k)的情况可以排除.剩下的三种情况, (min - k, max - k), (min + k, max + k) 和 (min + k, max - k),后两种等价于原值max

LC 302. Smallest Rectangle Enclosing Black Pixels【lock, hard】

An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black pixels are connected, i.e., there is only one black region. Pixels are connected horizontally and vertically. Given the location (x, y) of one of the

LC 988. Smallest String Starting From Leaf

Given the root of a binary tree, each node has a value from 0 to 25 representing the letters 'a' to 'z': a value of 0 represents 'a', a value of 1represents 'b', and so on. Find the lexicographically smallest string that starts at a leaf of this tree

908. Smallest Range I - LeetCode

Description: Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and add x to A[i]. After this process, we have some array B. Return the smallest possible difference between the maximum value of B and the minim

Leetcode-908 Smallest Range I(最小差值 I)

1 class Solution 2 { 3 public: 4 int smallestRangeI(vector<int>& A, int K) 5 { 6 int Max = INT_MIN; 7 int Min = INT_MAX; 8 for(auto d:A) 9 { 10 if(d > Max) 11 Max = d; 12 if(d < Min) 13 Min = d; 14 } 15 16 if(Min+2*K>=Max) 17 return 0;

Smallest Range II

2020-01-21 21:43:52 问题描述: 问题求解: 这个题目还是有点难度的,感觉很巧妙也很难想到. 整体的思路如下: 1. 首先原问题等价于 +0 / + 2*K 2. 那么res = Max - Min 3. 不断更新Max,Min期望得到更小的res public int smallestRangeII(int[] A, int K) { int n = A.length; Arrays.sort(A); int max = A[0]; int min = A[0]; for (