Skip to content

Commit 38cc3da

Browse files
committed
add for sort
1 parent f0557f8 commit 38cc3da

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com..houbb.leetcode.datastruct.array;
2+
3+
public class T252_meetingRoom_V1_BF {
4+
5+
public static boolean canAttendMeetings(int[][] intervals) {
6+
int n = intervals.length;
7+
8+
// 双重循环检查每一对会议是否重叠
9+
for (int i = 0; i < n; i++) {
10+
for (int j = i + 1; j < n; j++) {
11+
// 检查会议 intervals[i] 和 intervals[j] 是否重叠
12+
if (intervals[i][1] > intervals[j][0] && intervals[j][1] > intervals[i][0]) {
13+
return false; // 找到重叠会议,返回 false
14+
}
15+
}
16+
}
17+
18+
// 没有重叠会议
19+
return true;
20+
}
21+
22+
public static void main(String[] args) {
23+
int[][] intervals1 = {{0, 30}, {5, 10}, {15, 20}};
24+
System.out.println("Test case 1: " + canAttendMeetings(intervals1)); // 输出:false
25+
26+
int[][] intervals2 = {{7, 10}, {2, 4}};
27+
System.out.println("Test case 2: " + canAttendMeetings(intervals2)); // 输出:true
28+
}
29+
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com..houbb.leetcode.datastruct.array;
2+
3+
import java.util.Arrays;
4+
import java.util.Comparator;
5+
6+
public class T252_meetingRoom_V2_Sort {
7+
8+
public static boolean canAttendMeetings(int[][] intervals) {
9+
int n = intervals.length;
10+
11+
Arrays.sort(intervals, Comparator.comparingInt(o -> o[0]));
12+
13+
// 双重循环检查每一对会议是否重叠
14+
for (int i = 1; i < n; i++) {
15+
// 本次开始 上一次还没有结束
16+
if(intervals[i][0] < intervals[i-1][1]) {
17+
return false;
18+
}
19+
}
20+
21+
// 没有重叠会议
22+
return true;
23+
}
24+
25+
public static void main(String[] args) {
26+
int[][] intervals1 = {{0, 30}, {5, 10}, {15, 20}};
27+
System.out.println("Test case 1: " + canAttendMeetings(intervals1)); // 输出:false
28+
29+
int[][] intervals2 = {{7, 10}, {2, 4}};
30+
System.out.println("Test case 2: " + canAttendMeetings(intervals2)); // 输出:true
31+
}
32+
33+
}

0 commit comments

Comments
 (0)