返回介绍

solution / 0400-0499 / 0435.Non-overlapping Intervals / README_EN

发布于 2024-06-17 01:04:00 字数 5071 浏览 0 评论 0 收藏 0

435. Non-overlapping Intervals

中文文档

Description

Given an array of intervals intervals where intervals[i] = [starti, endi], return _the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping_.

 

Example 1:

Input: intervals = [[1,2],[2,3],[3,4],[1,3]]
Output: 1
Explanation: [1,3] can be removed and the rest of the intervals are non-overlapping.

Example 2:

Input: intervals = [[1,2],[1,2],[1,2]]
Output: 2
Explanation: You need to remove two [1,2] to make the rest of the intervals non-overlapping.

Example 3:

Input: intervals = [[1,2],[2,3]]
Output: 0
Explanation: You don't need to remove any of the intervals since they're already non-overlapping.

 

Constraints:

  • 1 <= intervals.length <= 105
  • intervals[i].length == 2
  • -5 * 104 <= starti < endi <= 5 * 104

Solutions

Solution 1

class Solution:
  def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
    intervals.sort(key=lambda x: x[1])
    ans, t = 0, intervals[0][1]
    for s, e in intervals[1:]:
      if s >= t:
        t = e
      else:
        ans += 1
    return ans
class Solution {
  public int eraseOverlapIntervals(int[][] intervals) {
    Arrays.sort(intervals, Comparator.comparingInt(a -> a[1]));
    int t = intervals[0][1], ans = 0;
    for (int i = 1; i < intervals.length; ++i) {
      if (intervals[i][0] >= t) {
        t = intervals[i][1];
      } else {
        ++ans;
      }
    }
    return ans;
  }
}
class Solution {
public:
  int eraseOverlapIntervals(vector<vector<int>>& intervals) {
    sort(intervals.begin(), intervals.end(), [](const auto& a, const auto& b) { return a[1] < b[1]; });
    int ans = 0, t = intervals[0][1];
    for (int i = 1; i < intervals.size(); ++i) {
      if (t <= intervals[i][0])
        t = intervals[i][1];
      else
        ++ans;
    }
    return ans;
  }
};
func eraseOverlapIntervals(intervals [][]int) int {
  sort.Slice(intervals, func(i, j int) bool {
    return intervals[i][1] < intervals[j][1]
  })
  t, ans := intervals[0][1], 0
  for i := 1; i < len(intervals); i++ {
    if intervals[i][0] >= t {
      t = intervals[i][1]
    } else {
      ans++
    }
  }
  return ans
}
function eraseOverlapIntervals(intervals: number[][]): number {
  intervals.sort((a, b) => a[1] - b[1]);
  let end = intervals[0][1],
    ans = 0;
  for (let i = 1; i < intervals.length; ++i) {
    let cur = intervals[i];
    if (end > cur[0]) {
      ans++;
    } else {
      end = cur[1];
    }
  }
  return ans;
}

Solution 2

class Solution:
  def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
    intervals.sort()
    d = [intervals[0][1]]
    for s, e in intervals[1:]:
      if s >= d[-1]:
        d.append(e)
      else:
        idx = bisect_left(d, s)
        d[idx] = min(d[idx], e)
    return len(intervals) - len(d)
class Solution {
  public int eraseOverlapIntervals(int[][] intervals) {
    Arrays.sort(intervals, (a, b) -> {
      if (a[0] != b[0]) {
        return a[0] - b[0];
      }
      return a[1] - b[1];
    });
    int n = intervals.length;
    int[] d = new int[n + 1];
    d[1] = intervals[0][1];
    int size = 1;
    for (int i = 1; i < n; ++i) {
      int s = intervals[i][0], e = intervals[i][1];
      if (s >= d[size]) {
        d[++size] = e;
      } else {
        int left = 1, right = size;
        while (left < right) {
          int mid = (left + right) >> 1;
          if (d[mid] >= s) {
            right = mid;
          } else {
            left = mid + 1;
          }
        }
        d[left] = Math.min(d[left], e);
      }
    }
    return n - size;
  }
}

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

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

发布评论

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