Press "Enter" to skip to content

Posts tagged as “range”

花花酱 LeetCode 729. My Calendar I

Problem:

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

Your class will have the 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Ā double bookingĀ happens when two events have some non-empty intersection (ie., there is some time that is common to both events.)

For each call to the methodĀ MyCalendar.book, returnĀ trueĀ if the event can be added to the calendar successfully without causing a double 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:Ā 

Binary Search

Solution1:

Brute Force: O(n^2)

C++

Solution 2:

Binary Search O(nlogn)

C++

Java

 

Related Problems:

花花酱 LeetCode 699. Falling Squares

é¢˜ē›®å¤§ę„ļ¼šę–¹å—č½äø‹åŽä¼šå †å åœØé‡å ēš„ę–¹å—ä¹‹äøŠļ¼Œé—®ęÆäø€å—ę–¹å—č½äø‹ä¹‹åŽęœ€é«˜ēš„ę–¹å—ēš„é«˜åŗ¦ę˜Æå¤šå°‘ļ¼Ÿ

Problem:

On an infinite number line (x-axis), we drop given squares in the order they are given.

TheĀ i-th square dropped (positions[i] = (left, side_length)) is a square with the left-most point beingĀ positions[i][0]Ā and sidelengthĀ positions[i][1].

The square is dropped with the bottom edge parallel to the number line, and from a higher height than all currently landed squares. We wait for each square to stick before dropping the next.

The squares are infinitely sticky on their bottom edge, and will remain fixed to any positive length surface they touch (either the number line or another square). Squares dropped adjacent to each other will not stick together prematurely.

 

Return a listĀ ansĀ of heights. Each heightĀ ans[i]Ā represents the current highest height of any square we have dropped, after dropping squares represented byĀ positions[0], positions[1], ..., positions[i].

Example 1:

Example 2:

Note:

 

  • 1 <= positions.length <= 1000.
  • 1 <= positions[i][0] <= 10^8.
  • 1 <= positions[i][1] <= 10^6.



Idea:

Range query with map

 

Solution:

C++ map

 

C++ vector without merge

C++ / vector with merge (slower)

 

Related Problems:

花花酱 LeetCode 715. Range Module

Problem:

A Range Module is a module that tracks ranges of numbers. Your task is to design and implement the following interfaces in an efficient manner.

 

  • addRange(int left, int right)Ā Adds the half-open intervalĀ [left, right), tracking every real number in that interval. Adding an interval that partially overlaps with currently tracked numbers should add any numbers in the intervalĀ [left, right)Ā that are not already tracked.
  • queryRange(int left, int right)Ā Returns true if and only if every real number in the intervalĀ [left, right)Ā is currently being tracked.
  • removeRange(int left, int right)Ā Stops tracking every real number currently being tracked in the intervalĀ [left, right).

Example 1:

Note:

  • A half open intervalĀ [left, right)Ā denotes all real numbersĀ left <= x < right.
  • 0 < left < right < 10^9Ā in all calls toĀ addRange, queryRange, removeRange.
  • The total number of calls toĀ addRangeĀ in a single test case is at mostĀ 1000.
  • The total number of calls toĀ queryRangeĀ in a single test case is at mostĀ 5000.
  • The total number of calls toĀ removeRangeĀ in a single test case is at mostĀ 1000.



Idea:

map / ordered ranges

Ā Ā 

 

Solution:

C++ / vector

C++ / map

Related Problems: