Press "Enter" to skip to content

Posts tagged as “medium”

花花酱 LeetCode 855. Exam Room

Problem

In an exam room, there areĀ NĀ seats in a single row, numberedĀ 0, 1, 2, ..., N-1.

When a student enters the room, they must sit in the seat that maximizes the distance to the closest person.Ā  If there are multiple such seats, they sit in the seat with the lowest number.Ā  (Also, if no one is in the room, then the student sits at seat number 0.)

Return a classĀ ExamRoom(int N)Ā that exposes two functions:Ā ExamRoom.seat()Ā returning anĀ intĀ representing what seat the student sat in, andĀ ExamRoom.leave(int p)Ā representing that the student in seat numberĀ pĀ now leaves the room.Ā  It is guaranteed that any calls toĀ ExamRoom.leave(p)Ā have a student sitting in seatĀ p.

Example 1:

Input: ["ExamRoom","seat","seat","seat","seat","leave","seat"], [[10],[],[],[],[],[4],[]]
Output: [null,0,9,4,2,null,5]
Explanation:
ExamRoom(10) -> null
seat() -> 0, no one is in the room, then the student sits at seat number 0.
seat() -> 9, the student sits at the last seat number 9.
seat() -> 4, the student sits at the last seat number 4.
seat() -> 2, the student sits at the last seat number 2.
leave(4) -> null
seat() -> 5, the studentā€‹ā€‹ā€‹ā€‹ā€‹ā€‹ā€‹ sits at the last seat number 5.

ā€‹ā€‹ā€‹ā€‹Note:

  1. 1 <= N <= 10^9
  2. ExamRoom.seat()Ā andĀ ExamRoom.leave()Ā will be called at mostĀ 10^4Ā times across all test cases.
  3. Calls toĀ ExamRoom.leave(p)Ā are guaranteed to have a student currently sitting in seat numberĀ p.

Solution: BST

Use a BST (ordered set) to track the current seatings.

Time Complexity:

init: O(1)

seat: O(P)

leave: O(logP)

Space complexity: O(P)

 

花花酱 LeetCode 882. Random Point in Non-overlapping Rectangles

Problem

Given a list ofĀ non-overlappingĀ axis-aligned rectanglesĀ rects, write a functionĀ pickĀ which randomly and uniformily picks anĀ integer pointĀ in the spaceĀ covered by the rectangles.

Note:

  1. AnĀ integer pointĀ is a point that has integer coordinates.
  2. A pointĀ on the perimeterĀ of a rectangle isĀ includedĀ in the space covered by the rectangles.
  3. ith rectangle =Ā rects[i]Ā =Ā [x1,y1,x2,y2], whereĀ [x1, y1]Ā are the integer coordinates of the bottom-left corner, andĀ [x2, y2]Ā are the integer coordinates of the top-right corner.
  4. length and width of each rectangle does not exceedĀ 2000.
  5. 1 <= rects.lengthĀ <= 100
  6. pickĀ return a point as an array of integer coordinatesĀ [p_x, p_y]
  7. pickĀ is called at mostĀ 10000Ā times.

Example 1:

Input: 
["Solution","pick","pick","pick"]
[[[[1,1,5,5]]],[],[],[]]
Output: 
[null,[4,1],[4,1],[3,3]]

Example 2:

Input: 
["Solution","pick","pick","pick","pick","pick"]
[[[[-2,-2,-1,-1],[1,0,3,0]]],[],[],[],[],[]]
Output: 
[null,[-1,-2],[2,0],[-2,-1],[3,0],[-2,-2]]

Explanation of Input Syntax:

The input is two lists:Ā the subroutines calledĀ and theirĀ arguments.Ā Solution‘sĀ constructor has one argument, the array of rectanglesĀ rects.Ā pickĀ has no arguments.Ā ArgumentsĀ areĀ always wrapped with a list, even if there aren’t any.

Solution: Binary Search

Same asĀ LeetCode 880. Random Pick with Weight

Use area of the rectangles as weights.

Time complexity: Init: O(n) Pick: O(logn)

Space complexity: O(n)

Related Problems

花花酱 LeetCode 880. Random Pick with Weight

Problem

Given an arrayĀ wĀ of positive integers, whereĀ w[i]Ā describes the weight of indexĀ i,Ā write a functionĀ pickIndexĀ which randomlyĀ picks an indexĀ in proportionĀ to its weight.

Note:

  1. 1 <= w.length <= 10000
  2. 1 <= w[i] <= 10^5
  3. pickIndexĀ will be called at mostĀ 10000Ā times.

Example 1:

Input: 
["Solution","pickIndex"]
[[[1]],[]]
Output: [null,0]

Example 2:

Input: 
["Solution","pickIndex","pickIndex","pickIndex","pickIndex","pickIndex"]
[[[1,3]],[],[],[],[],[]]
Output: [null,0,1,1,1,0]

Explanation of Input Syntax:

The input is two lists:Ā the subroutines calledĀ and theirĀ arguments.Ā Solution‘sĀ constructor has one argument, theĀ arrayĀ w.Ā pickIndexĀ has no arguments.Ā ArgumentsĀ areĀ always wrapped with a list, even if there aren’t any.

Solution: Binary Search

  1. Convert PDF to CDF
  2. Uniformly sample a value s in [1, sum(weights)].
  3. Use binary search to find first index such that PDF[index] >= s.

Time complexity: Init O(n), query O(logn)

Space complexity: O(n)

C++

Related Problems

花花酱 LeetCode 883. Generate Random Point in a Circle

Problem

Given the radius and x-y positions of the center of a circle, write a functionĀ randPointĀ whichĀ generates a uniform randomĀ point in the circle.

Note:

  1. input and output values areĀ inĀ floating-point.
  2. radius and x-y position of the center of the circle is passed into the class constructor.
  3. a point on the circumference of the circle is considered to beĀ in the circle.
  4. randPointĀ returnsĀ a size 2 array containing x-position and y-position of the random point, in that order.

Example 1:

Input: 
["Solution","randPoint","randPoint","randPoint"]
[[1,0,0],[],[],[]]
Output: [null,[-0.72939,-0.65505],[-0.78502,-0.28626],[-0.83119,-0.19803]]

Example 2:

Input: 
["Solution","randPoint","randPoint","randPoint"]
[[10,5,-7.5],[],[],[]]
Output: [null,[11.52438,-8.33273],[2.46992,-16.21705],[11.13430,-12.42337]]

Explanation of Input Syntax:

The input is two lists:Ā the subroutines calledĀ and theirĀ arguments.Ā Solution‘sĀ constructor has three arguments, the radius, x-position of the center, and y-position of the center of the circle.Ā randPointĀ has no arguments.Ā ArgumentsĀ areĀ always wrapped with a list, even if there aren’t any.

Ā 

Solution: Polar Coordinate

uniform sample an angle a: [0, 2*Pi)

uniform sample a radius r: [0, 1)

Number of random points in a cycle should be proportional to the square of distance to the center.

e.g. there are 4 times of points with distance d than points with distance d/2.

Thus sqrt(r) is uniformly distributed.

r’ = sqrt(r),

C++

花花酱 LeetCode 148. Sort List

Problem

Sort a linked list inĀ O(nĀ logĀ n) time using constant space complexity.

Example 1:

Input: 4->2->1->3
Output: 1->2->3->4

Example 2:

Input: -1->5->3->4->0
Output: -1->0->3->4->5

Solution: Merge Sort

Top-down (recursion)

Time complexity: O(nlogn)

Space complexity: O(logn)

C++

Java

Python3

 

bottom up

Time complexity: O(nlogn)

Space complexity: O(1)