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="" of="" elements="" code="">
class Solution {
public:
vector smallestRange(vector>& nums) {
}
};
######思路:将所有数排序,并存储坐标,比对值与坐标找出最小的范围
class Solution {
public:
vector smallestRange(vector>& nums) {
vector res;
vector> v;
unordered_map m;
for(int i=0;iv[right].first-v[left].first)
{
diff=v[right].first-v[left].first;
res={v[left].first,v[right].first};
}
if(--m[v[left].second]==0) --cnt;
++left;
}
}
return res;
}
};
近期评论