Leetcode: Meeting Rooms
2023-09-11 14:14:07 时间
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all meetings. For example, Given [[0, 30],[5, 10],[15, 20]], return false.
Interval as an array:
1 class Solution { 2 public boolean canAttendMeetings(int[][] intervals) { 3 if (intervals.length < 1) return true; 4 5 Arrays.sort(intervals, (i1, i2) -> Integer.compare(i1[0], i2[0])); 6 7 for (int i = 1; i < intervals.length; i ++) { 8 if (intervals[i][0] < intervals[i - 1][1]) return false; 9 } 10 return true; 11 } 12 }
Previous interval as an object:
Implement a Comparator<Interval>
Syntax: don't forget the public sign when defining a function
1 /** 2 * Definition for an interval. 3 * public class Interval { 4 * int start; 5 * int end; 6 * Interval() { start = 0; end = 0; } 7 * Interval(int s, int e) { start = s; end = e; } 8 * } 9 */ 10 public class Solution { 11 public boolean canAttendMeetings(Interval[] intervals) { 12 if (intervals==null || intervals.length==0 || intervals.length==1) return true; 13 Comparator<Interval> comp = new Comparator<Interval>() { 14 public int compare(Interval i1, Interval i2) { 15 return (i1.start==i2.start)? i1.end-i2.end : i1.start-i2.start; 16 } 17 }; 18 19 Arrays.sort(intervals, comp); 20 Interval pre = intervals[0]; 21 for (int i=1; i<intervals.length; i++) { 22 Interval cur = intervals[i]; 23 if (cur.start < pre.end) return false; 24 pre = cur; 25 } 26 return true; 27 } 28 }
相关文章
- Leetcode: Longest Substring with At Least K Repeating Characters
- Leetcode: Unique Paths
- Leetcode Best Time to Buy and Sell Stock
- [leetcode] 4. Median of Two Sorted Arrays
- LeetCode高频题:n杯溶液按照顺序排成一排,可以混合相邻2杯,合并代价/时间是两杯质量和,经过n-1次合并为1杯,最小时间/代价是多少
- 【Leetcode】29.两数整除(中等)
- [LeetCode] Valid Sudoku
- 【leetcode】力扣 --- 日积月累,每日一题——5 二分查找
- [LeetCode] 1326. Minimum Number of Taps to Open to Water a Garden 灌溉花园的最少水龙头数目
- [LeetCode] 814. Binary Tree Pruning 二叉树修剪
- [LeetCode] Split Concatenated Strings 分割串联字符串
- [LeetCode] 369. Plus One Linked List 链表加一运算
- leetcode 114. Flatten Binary Tree to Linked List 二叉树展开为链表(简单)
- leetcode 142. Linked List Cycle II 环形链表 II