Press "Enter" to skip to content

Posts tagged as “sweep line”

花花酱 LeetCode 731. My Calendar II – 花花酱

Problem:Ā 

Implement aĀ MyCalendarTwoĀ class to store your events. A new event can be added if adding the event will not cause aĀ tripleĀ booking.

Your class will have one method,Ā book(int start, int end). Formally, this represents a booking on the half open intervalĀ [start, end), the range of real numbersĀ xĀ such thatĀ start <= x < end.

AĀ triple bookingĀ happens whenĀ threeĀ events have some non-empty intersection (ie., there is some time that is common to all 3 events.)

For each call to the methodĀ MyCalendar.book, returnĀ trueĀ if the event can be added to the calendar successfully without causing aĀ tripleĀ booking. Otherwise, returnĀ falseĀ and do not add the event to the calendar.

Your class will be called like this:Ā MyCalendar cal = new MyCalendar();Ā MyCalendar.book(start, end)

Example 1:

Note:

 

  • The number of calls toĀ MyCalendar.bookĀ per test case will be at mostĀ 1000.
  • In calls toĀ MyCalendar.book(start, end),Ā startĀ andĀ endĀ are integers in the rangeĀ [0, 10^9].

Idea:

Brute Force

Solution1:

Brute Force

Time Complexity: O(n^2)

Space Complexity: O(n)

 

Java

Python

 

Solution 2:

Counting

Time Complexity: O(n^2logn)

Space Complexity: O(n)

 

Related Problems:

花花酱 LeetCode 56. Merge Intervals

Problem:

Given a collection of intervals, merge all overlapping intervals.

For example,
GivenĀ [1,3],[2,6],[8,10],[15,18],
returnĀ [1,6],[8,10],[15,18].



Idea:

Sweep line

Solution:

C++

Python

 

Related Problems:

花花酱 LeetCode 218. The Skyline Problem

Problem:

A city’s skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you areĀ given the locations and height of all the buildingsĀ as shown on a cityscape photo (Figure A), write a program toĀ output the skylineĀ formed by these buildings collectively (Figure B).

BuildingsĀ Skyline Contour

The geometric information of each building is represented by a triplet of integersĀ [Li, Ri, Hi], whereĀ LiĀ andĀ RiĀ are the x coordinates of the left and right edge of the ith building, respectively, andĀ HiĀ is its height. It is guaranteed thatĀ 0 ā‰¤ Li, Ri ā‰¤ INT_MAX,Ā 0 < Hi ā‰¤ INT_MAX, andĀ Ri - Li > 0. You may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height 0.

For instance, the dimensions of all buildings in Figure A are recorded as:Ā [ [2 9 10], [3 7 15], [5 12 12], [15 20 10], [19 24 8] ]Ā .

The output is a list of “key points” (red dots in Figure B) in the format ofĀ [ [x1,y1], [x2, y2], [x3, y3], ... ]Ā that uniquely defines a skyline.Ā A key point is the left endpoint of a horizontal line segment. Note that the last key point, where the rightmost building ends, is merely used to mark the termination of the skyline, and always has zero height. Also, the ground in between any two adjacent buildings should be considered part of the skyline contour.

For instance, the skyline in Figure B should be represented as:[ [2 10], [3 15], [7 12], [12 0], [15 10], [20 8], [24, 0] ].

Notes:

  • The number of buildings in any input list is guaranteed to be in the rangeĀ [0, 10000].
  • The input list is already sorted in ascending order by the left x positionĀ Li.
  • The output list must be sorted by the x position.
  • There must be no consecutive horizontal lines of equal height in the output skyline. For instance,Ā [...[2 3], [4 5], [7 5], [11 5], [12 7]...]Ā is not acceptable; the three lines of height 5 should be merged into one in the final output as such:Ā [...[2 3], [4 5], [12 7], ...]

Ā 

Idea:

Sweep line



Time Complexity:

O(nlogn)

Space Complexity:

O(n)

Solution1: HeapĀ 

C++

Java

Solution 2: Multiset

C++