返回介绍

solution / 0700-0799 / 0757.Set Intersection Size At Least Two / README_EN

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

757. Set Intersection Size At Least Two

中文文档

Description

You are given a 2D integer array intervals where intervals[i] = [starti, endi] represents all the integers from starti to endi inclusively.

A containing set is an array nums where each interval from intervals has at least two integers in nums.

  • For example, if intervals = [[1,3], [3,7], [8,9]], then [1,2,4,7,8,9] and [2,3,4,8,9] are containing sets.

Return _the minimum possible size of a containing set_.

 

Example 1:

Input: intervals = [[1,3],[3,7],[8,9]]
Output: 5
Explanation: let nums = [2, 3, 4, 8, 9].
It can be shown that there cannot be any containing array of size 4.

Example 2:

Input: intervals = [[1,3],[1,4],[2,5],[3,5]]
Output: 3
Explanation: let nums = [2, 3, 4].
It can be shown that there cannot be any containing array of size 2.

Example 3:

Input: intervals = [[1,2],[2,3],[2,4],[4,5]]
Output: 5
Explanation: let nums = [1, 2, 3, 4, 5].
It can be shown that there cannot be any containing array of size 4.

 

Constraints:

  • 1 <= intervals.length <= 3000
  • intervals[i].length == 2
  • 0 <= starti < endi <= 108

Solutions

Solution 1

class Solution:
  def intersectionSizeTwo(self, intervals: List[List[int]]) -> int:
    intervals.sort(key=lambda x: (x[1], -x[0]))
    s = e = -1
    ans = 0
    for a, b in intervals:
      if a <= s:
        continue
      if a > e:
        ans += 2
        s, e = b - 1, b
      else:
        ans += 1
        s, e = e, b
    return ans
class Solution {
  public int intersectionSizeTwo(int[][] intervals) {
    Arrays.sort(intervals, (a, b) -> a[1] == b[1] ? b[0] - a[0] : a[1] - b[1]);
    int ans = 0;
    int s = -1, e = -1;
    for (int[] v : intervals) {
      int a = v[0], b = v[1];
      if (a <= s) {
        continue;
      }
      if (a > e) {
        ans += 2;
        s = b - 1;
        e = b;
      } else {
        ans += 1;
        s = e;
        e = b;
      }
    }
    return ans;
  }
}
class Solution {
public:
  int intersectionSizeTwo(vector<vector<int>>& intervals) {
    sort(intervals.begin(), intervals.end(), [&](vector<int>& a, vector<int>& b) {
      return a[1] == b[1] ? a[0] > b[0] : a[1] < b[1];
    });
    int ans = 0;
    int s = -1, e = -1;
    for (auto& v : intervals) {
      int a = v[0], b = v[1];
      if (a <= s) continue;
      if (a > e) {
        ans += 2;
        s = b - 1;
        e = b;
      } else {
        ans += 1;
        s = e;
        e = b;
      }
    }
    return ans;
  }
};
func intersectionSizeTwo(intervals [][]int) int {
  sort.Slice(intervals, func(i, j int) bool {
    a, b := intervals[i], intervals[j]
    if a[1] == b[1] {
      return a[0] > b[0]
    }
    return a[1] < b[1]
  })
  ans := 0
  s, e := -1, -1
  for _, v := range intervals {
    a, b := v[0], v[1]
    if a <= s {
      continue
    }
    if a > e {
      ans += 2
      s, e = b-1, b
    } else {
      ans += 1
      s, e = e, b
    }
  }
  return ans
}

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

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

发布评论

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