Press "Enter" to skip to content

Posts published in July 2018

花花酱 LeetCode 881. Random Flip Matrix

Problem

You are given the number of rowsĀ n_rowsĀ and number of columnsĀ n_colsĀ of aĀ 2DĀ binary matrixĀ where all values are initially 0.Ā Write a functionĀ flipĀ which choosesĀ a 0 valueĀ uniformly at random,Ā changes it to 1,Ā and then returns the positionĀ [row.id, col.id]Ā of that value. Also, write a functionĀ resetĀ which sets all values back to 0.Ā Try to minimize the number of calls to system’s Math.random()Ā and optimize the time andĀ space complexity.

Note:

  1. 1 <= n_rows, n_colsĀ <= 10000
  2. 0 <= row.id < n_rowsĀ andĀ 0 <= col.id < n_cols
  3. flipĀ will not be called when the matrix has noĀ 0 values left.
  4. the total number of calls toĀ flipĀ andĀ resetĀ will not exceedĀ 1000.

Example 1:

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

Example 2:

Input: 
["Solution","flip","flip","reset","flip"]
[[1,2],[],[],[],[]]
Output: [null,[0,0],[0,1],null,[0,0]]

Explanation of Input Syntax:

The input is two lists:Ā the subroutines calledĀ and theirĀ arguments.Ā Solution‘s constructorĀ has two arguments,Ā n_rowsĀ andĀ n_cols.Ā flipĀ andĀ resetĀ haveĀ noĀ arguments.Ā ArgumentsĀ areĀ always wrapped with a list, even if there aren’t any.

Solution 1: Hashtable + Resample

Time complexity: O(|flip|) = O(1000) = O(1)

Space complexity: O(|flip|) =Ā O(1000) = O(1)

Solution 2:Ā Fisherā€“Yates shuffle

Generate a random shuffle of 0 to n – 1, one number at a time.

Time complexity: flip: O(1)

Space complexity: O(|flip|) = O(1000) = O(1)

C++

 

花花酱 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 879. Profitable Schemes

Problem

There are G people in a gang, and a list of various crimes they could commit.

TheĀ i-th crime generates aĀ profit[i]Ā and requiresĀ group[i]Ā gang members to participate.

If a gang member participates in one crime, that member can’t participate in another crime.

Let’s call aĀ profitableĀ schemeĀ any subset of these crimes that generates at leastĀ PĀ profit, and the total number of gang members participating in that subset of crimes is at most G.

How many schemes can be chosen?Ā  Since the answer may be veryĀ large,Ā return it moduloĀ 10^9 + 7.

Example 1:

Input: G = 5, P = 3, group = [2,2], profit = [2,3]
Output: 2
Explanation: 
To make a profit of at least 3, the gang could either commit crimes 0 and 1, or just crime 1.
In total, there are 2 schemes.

Example 2:

Input: G = 10, P = 5, group = [2,3,5], profit = [6,7,8]
Output: 7
Explanation: 
To make a profit of at least 5, the gang could commit any crimes, as long as they commit one.
There are 7 possible schemes: (0), (1), (2), (0,1), (0,2), (1,2), and (0,1,2).

Note:

  1. 1 <= G <= 100
  2. 0 <= P <= 100
  3. 1 <= group[i] <= 100
  4. 0 <= profit[i] <= 100
  5. 1 <= group.length = profit.length <= 100

Solution: DP

Time complexity: O(KPG)

Space complexity: O(KPG)

C++

Space complexity: O(PG)

v1: Dimension reduction by copying.

v2: Dimension reduction by using rolling array.

 

花花酱 LeetCode 878. Nth Magical Number

Problem

A positive integerĀ isĀ magicalĀ if it is divisible by eitherĀ AĀ orĀ B.

Return theĀ N-th magical number.Ā  Since the answer may be very large,Ā return it moduloĀ 10^9 + 7.

Example 1:

Input: N = 1, A = 2, B = 3
Output: 2

Example 2:

Input: N = 4, A = 2, B = 3
Output: 6

Example 3:

Input: N = 5, A = 2, B = 4
Output: 10

Example 4:

Input: N = 3, A = 6, B = 4
Output: 8

Note:

  1. 1 <= NĀ <= 10^9
  2. 2 <= AĀ <= 40000
  3. 2 <= BĀ <= 40000

Solution: Math + Binary Search

Let n denote the number of numbers <= X that are divisible by eitherĀ AĀ orĀ B.

n = X / A + X / B – X / lcm(A, B) = X / A + X / B – X / (A * B / gcd(A, B))

Binary search for the smallest X such that n >= N

Time complexity: O(log(1e9*4e5)

Space complexity: O(1)