From a9be61f016e1816c097d467ffd7a1a1ae5a4f232 Mon Sep 17 00:00:00 2001 From: huangge1199 Date: Mon, 6 Sep 2021 13:07:58 +0800 Subject: [PATCH] =?UTF-8?q?435:=E6=97=A0=E9=87=8D=E5=8F=A0=E5=8C=BA?= =?UTF-8?q?=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../editor/cn/NonOverlappingIntervals.java | 74 +++++++++++++++++++ .../cn/doc/content/NonOverlappingIntervals.md | 39 ++++++++++ 2 files changed, 113 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/NonOverlappingIntervals.java create mode 100644 src/main/java/leetcode/editor/cn/doc/content/NonOverlappingIntervals.md diff --git a/src/main/java/leetcode/editor/cn/NonOverlappingIntervals.java b/src/main/java/leetcode/editor/cn/NonOverlappingIntervals.java new file mode 100644 index 0000000..5afca07 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/NonOverlappingIntervals.java @@ -0,0 +1,74 @@ +//给定一个区间的集合,找到需要移除区间的最小数量,使剩余区间互不重叠。 +// +// 注意: +// +// +// 可以认为区间的终点总是大于它的起点。 +// 区间 [1,2] 和 [2,3] 的边界相互“接触”,但没有相互重叠。 +// +// +// 示例 1: +// +// +//输入: [ [1,2], [2,3], [3,4], [1,3] ] +// +//输出: 1 +// +//解释: 移除 [1,3] 后,剩下的区间没有重叠。 +// +// +// 示例 2: +// +// +//输入: [ [1,2], [1,2], [1,2] ] +// +//输出: 2 +// +//解释: 你需要移除两个 [1,2] 来使剩下的区间没有重叠。 +// +// +// 示例 3: +// +// +//输入: [ [1,2], [2,3] ] +// +//输出: 0 +// +//解释: 你不需要移除任何区间,因为它们已经是无重叠的了。 +// +// Related Topics 贪心 数组 动态规划 排序 👍 491 👎 0 + +package leetcode.editor.cn; + +import java.util.Arrays; +import java.util.Comparator; + +//435:无重叠区间 +class NonOverlappingIntervals { + public static void main(String[] args) { + //测试代码 + Solution solution = new NonOverlappingIntervals().new Solution(); + } + + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int eraseOverlapIntervals(int[][] intervals) { + if (intervals.length == 0) { + return 0; + } + Arrays.sort(intervals, Comparator.comparingInt(interval -> interval[1])); + int num = intervals[0][1]; + int count = 1; + for (int i = 1; i < intervals.length; ++i) { + if (intervals[i][0] >= num) { + ++count; + num = intervals[i][1]; + } + } + return intervals.length - count; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/doc/content/NonOverlappingIntervals.md b/src/main/java/leetcode/editor/cn/doc/content/NonOverlappingIntervals.md new file mode 100644 index 0000000..0a37277 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/doc/content/NonOverlappingIntervals.md @@ -0,0 +1,39 @@ +

给定一个区间的集合,找到需要移除区间的最小数量,使剩余区间互不重叠。

+ +

注意:

+ +
    +
  1. 可以认为区间的终点总是大于它的起点。
  2. +
  3. 区间 [1,2] 和 [2,3] 的边界相互“接触”,但没有相互重叠。
  4. +
+ +

示例 1:

+ +
+输入: [ [1,2], [2,3], [3,4], [1,3] ]
+
+输出: 1
+
+解释: 移除 [1,3] 后,剩下的区间没有重叠。
+
+ +

示例 2:

+ +
+输入: [ [1,2], [1,2], [1,2] ]
+
+输出: 2
+
+解释: 你需要移除两个 [1,2] 来使剩下的区间没有重叠。
+
+ +

示例 3:

+ +
+输入: [ [1,2], [2,3] ]
+
+输出: 0
+
+解释: 你不需要移除任何区间,因为它们已经是无重叠的了。
+
+
Related Topics
  • 贪心
  • 数组
  • 动态规划
  • 排序

  • 👍 491
  • 👎 0
  • \ No newline at end of file