區間問題

區間題型的解題教學,6 題。先排序,再決定是合併、插入還是找重疊——順序弄對,題目就解一半了。

Meeting Rooms III 解題教學示意圖|LeetCode 2402

Meeting Rooms III 解題教學:3 種解法完整比較|LeetCode 2402 Best Practice

ou are given an integer n. There are n rooms numbered from 0 to n – 1.

You are given a 2D integer array meetings where meetings[i] = [starti, endi] means that a meeting will be held during the half-closed time interval [starti, endi). All the values of starti are unique.

Meetings are allocated to rooms in the following manner:

Each meeting will take place in the unused room with the lowest number.
If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the same duration as the original meeting.
When a room becomes unused, meetings that have an earlier original start time should be given the room.
Return the number of the room that held the most meetings. If there are multiple rooms, return the room with the lowest number.

A half-closed interval [a, b) is the interval between a and b including a and not including b.

Meeting Rooms III 解題教學:3 種解法完整比較|LeetCode 2402 Best Practice Read More »

Leetcode, 技術文章, , ,
Meeting Rooms II 解題教學示意圖|LeetCode 253

Meeting Rooms II 解題教學:3 種解法完整比較|LeetCode 253 Best Practice

Given an array of meeting time intervals intervals where intervals[i] = [starti, endi], return the minimum number of conference rooms required.

Meeting Rooms II 解題教學:3 種解法完整比較|LeetCode 253 Best Practice Read More »

Leetcode, 技術文章, , ,
Insert Interval 解題教學示意圖|LeetCode 57

Insert Interval 解題教學:3 種解法完整比較|LeetCode 57 Best Practice

You are given an array of non-overlapping intervals intervals where intervals[i] = [starti, endi] represent the start and the end of the ith interval and intervals is sorted in ascending order by starti. You are also given an interval newInterval = [start, end] that represents the start and end of another interval.

Insert newInterval into intervals such that intervals is still sorted in ascending order by starti and intervals still does not have any overlapping intervals (merge overlapping intervals if necessary).

Return intervals after the insertion.

Note that you don’t need to modify intervals in-place. You can make a new array and return it.

Insert Interval 解題教學:3 種解法完整比較|LeetCode 57 Best Practice Read More »

Leetcode, 技術文章, , ,