Press "Enter" to skip to content

Posts tagged as “overlap”

花花酱 LeetCode 836. Rectangle Overlap

Problem

A rectangle isĀ represented as aĀ listĀ [x1, y1, x2, y2], whereĀ (x1, y1)Ā are the coordinates of its bottom-left corner, andĀ (x2,Ā y2)Ā are the coordinates of its top-right corner.

Two rectangles overlap if the area of their intersection is positive.Ā  To be clear, two rectangles that only touch at the corner or edges do not overlap.

Given two (axis-aligned) rectangles, return whetherĀ they overlap.

Example 1:

Input: rec1 = [0,0,2,2], rec2 = [1,1,3,3]
Output: true

Example 2:

Input: rec1 = [0,0,1,1], rec2 = [1,0,2,1]
Output: false

Notes:

  1. Both rectanglesĀ rec1Ā andĀ rec2Ā are lists of 4 integers.
  2. All coordinates in rectangles will be betweenĀ -10^9Ā andĀ 10^9.

Solution: Geometry

Time complexity: O(1)

Space complexity: O(1)

 

花花酱 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: