返回介绍

solution / 0600-0699 / 0632.Smallest Range Covering Elements from K Lists / README_EN

发布于 2024-06-17 01:03:36 字数 6196 浏览 0 评论 0 收藏 0

632. Smallest Range Covering Elements from K Lists

中文文档

Description

You have k lists of sorted integers in non-decreasing 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: nums = [[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].

Example 2:

Input: nums = [[1,2,3],[1,2,3],[1,2,3]]
Output: [1,1]

 

Constraints:

  • nums.length == k
  • 1 <= k <= 3500
  • 1 <= nums[i].length <= 50
  • -105 <= nums[i][j] <= 105
  • nums[i] is sorted in non-decreasing order.

Solutions

Solution 1

class Solution:
  def smallestRange(self, nums: List[List[int]]) -> List[int]:
    t = [(x, i) for i, v in enumerate(nums) for x in v]
    t.sort()
    cnt = Counter()
    ans = [-inf, inf]
    j = 0
    for i, (b, v) in enumerate(t):
      cnt[v] += 1
      while len(cnt) == len(nums):
        a = t[j][0]
        x = b - a - (ans[1] - ans[0])
        if x < 0 or (x == 0 and a < ans[0]):
          ans = [a, b]
        w = t[j][1]
        cnt[w] -= 1
        if cnt[w] == 0:
          cnt.pop(w)
        j += 1
    return ans
class Solution {
  public int[] smallestRange(List<List<Integer>> nums) {
    int n = 0;
    for (var v : nums) {
      n += v.size();
    }
    int[][] t = new int[n][2];
    int k = nums.size();
    for (int i = 0, j = 0; i < k; ++i) {
      for (int x : nums.get(i)) {
        t[j++] = new int[] {x, i};
      }
    }
    Arrays.sort(t, (a, b) -> a[0] - b[0]);
    int j = 0;
    Map<Integer, Integer> cnt = new HashMap<>();
    int[] ans = new int[] {-1000000, 1000000};
    for (int[] e : t) {
      int b = e[0];
      int v = e[1];
      cnt.put(v, cnt.getOrDefault(v, 0) + 1);
      while (cnt.size() == k) {
        int a = t[j][0];
        int w = t[j][1];
        int x = b - a - (ans[1] - ans[0]);
        if (x < 0 || (x == 0 && a < ans[0])) {
          ans[0] = a;
          ans[1] = b;
        }
        cnt.put(w, cnt.get(w) - 1);
        if (cnt.get(w) == 0) {
          cnt.remove(w);
        }
        ++j;
      }
    }
    return ans;
  }
}
class Solution {
public:
  vector<int> smallestRange(vector<vector<int>>& nums) {
    int n = 0;
    for (auto& v : nums) n += v.size();
    vector<pair<int, int>> t(n);
    int k = nums.size();
    for (int i = 0, j = 0; i < k; ++i) {
      for (int v : nums[i]) {
        t[j++] = {v, i};
      }
    }
    sort(t.begin(), t.end());
    int j = 0;
    unordered_map<int, int> cnt;
    vector<int> ans = {-1000000, 1000000};
    for (int i = 0; i < n; ++i) {
      int b = t[i].first;
      int v = t[i].second;
      ++cnt[v];
      while (cnt.size() == k) {
        int a = t[j].first;
        int w = t[j].second;
        int x = b - a - (ans[1] - ans[0]);
        if (x < 0 || (x == 0 && a < ans[0])) {
          ans[0] = a;
          ans[1] = b;
        }
        if (--cnt[w] == 0) {
          cnt.erase(w);
        }
        ++j;
      }
    }
    return ans;
  }
};
func smallestRange(nums [][]int) []int {
  t := [][]int{}
  for i, x := range nums {
    for _, v := range x {
      t = append(t, []int{v, i})
    }
  }
  sort.Slice(t, func(i, j int) bool { return t[i][0] < t[j][0] })
  ans := []int{-1000000, 1000000}
  j := 0
  cnt := map[int]int{}
  for _, x := range t {
    b, v := x[0], x[1]
    cnt[v]++
    for len(cnt) == len(nums) {
      a, w := t[j][0], t[j][1]
      x := b - a - (ans[1] - ans[0])
      if x < 0 || (x == 0 && a < ans[0]) {
        ans[0], ans[1] = a, b
      }
      cnt[w]--
      if cnt[w] == 0 {
        delete(cnt, w)
      }
      j++
    }
  }
  return ans
}
impl Solution {
  pub fn smallest_range(nums: Vec<Vec<i32>>) -> Vec<i32> {
    let mut t = vec![];
    for (i, x) in nums.iter().enumerate() {
      for &v in x {
        t.push((v, i));
      }
    }
    t.sort_unstable();
    let (mut ans, n) = (vec![-1000000, 1000000], nums.len());
    let mut j = 0;
    let mut cnt = std::collections::HashMap::new();

    for (b, v) in t.iter() {
      let (b, v) = (*b, *v);
      if let Some(x) = cnt.get_mut(&v) {
        *x += 1;
      } else {
        cnt.insert(v, 1);
      }
      while cnt.len() == n {
        let (a, w) = t[j];
        let x = b - a - (ans[1] - ans[0]);
        if x < 0 || (x == 0 && a < ans[0]) {
          ans = vec![a, b];
        }
        if let Some(x) = cnt.get_mut(&w) {
          *x -= 1;
        }
        if cnt[&w] == 0 {
          cnt.remove(&w);
        }
        j += 1;
      }
    }
    ans
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文