Press "Enter" to skip to content

Posts tagged as “binary search”

花花酱 LeetCode 668. Kth Smallest Number in Multiplication Table

Problem

Nearly every one have used theĀ Multiplication Table. But could you find out theĀ k-thĀ smallest number quickly from the multiplication table?

Given the heightĀ mĀ and the lengthĀ nĀ of aĀ m * nĀ Multiplication Table, and a positive integerĀ k, you need to return theĀ k-thĀ smallest number in this table.

Example 1:

Input: m = 3, n = 3, k = 5
Output: 
Explanation: 
The Multiplication Table:
1	2	3
2	4	6
3	6	9

The 5-th smallest number is 3 (1, 2, 2, 3, 3).

Example 2:

Input: m = 2, n = 3, k = 6
Output: 
Explanation: 
The Multiplication Table:
1	2	3
2	4	6

The 6-th smallest number is 6 (1, 2, 2, 3, 4, 6).

Note:

  1. TheĀ mĀ andĀ nĀ will be in the range [1, 30000].
  2. TheĀ kĀ will be in the range [1, m * n]

Ā 

Solution 1: Nth element (MLE)

Time complexity: O(mn)

Space complexity: O(mn)

C++

Solution2 : Binary Search

Find first x such that there are k elements less or equal to x in the table.

Time complexity: O(m*log(m*n))

Space complexity: O(1)

C++

Java

Python3

RelatedĀ Problems

花花酱 LeetCode 887. Super Egg Drop

Problem

You are givenĀ KĀ eggs, and you have access to a building withĀ NĀ floors fromĀ 1Ā toĀ N.

Each egg is identical in function, and if an egg breaks, you cannot drop itĀ again.

You know that there exists a floorĀ FĀ withĀ 0 <= F <= NĀ such that any egg dropped at a floor higher thanĀ FĀ will break, and any egg dropped at or below floorĀ FĀ will not break.

EachĀ move, you may take an egg (if you have an unbroken one) and drop it from any floorĀ XĀ (withĀ 1 <= X <= N).

Your goal is to knowĀ with certaintyĀ what the value ofĀ FĀ is.

What is the minimum number of moves that you need to know with certaintyĀ whatĀ FĀ is, regardless of the initial value ofĀ F?

Example 1:

Input: K = 1, N = 2
Output: 2
Explanation: 
Drop the egg from floor 1.  If it breaks, we know with certainty that F = 0.
Otherwise, drop the egg from floor 2.  If it breaks, we know with certainty that F = 1.
If it didn't break, then we know with certainty F = 2.
Hence, we needed 2 moves in the worst case to know what F is with certainty.

Example 2:

Input: K = 2, N = 6
Output: 3

Example 3:

Input: K = 3, N = 14
Output: 4

Note:

  1. 1 <= K <= 100
  2. 1 <= N <= 10000

Solution 1: Recursion with Memorization (TLE)

Ā 

Ā 

dp[k][n] := min number of moves to test n floors with k eggs.

Base cases:

dp[0][n] = 0 # no eggs left.
dp[1][n] = nĀ  # one egg, need to test every floor.

Transition:

dp[k][n] = min(1 + max(dp[k][i – 1], dp[k – 1][n – i])) 1 <= i <= n

Time complexity: O(k*n^2)

Space complexity: O(k*n)

Solution 2: Solution 1 + Binary Search

Time complexity: O(k*n*logn)

Space complexity: O(k*n)

C++

Ā 

Ā 

花花酱 SP5 Binary Search

Template:

Time complexity: O(log(r-l)) * O(f(m) + g(m))

Space complexity: O(1)

 

Slides:

Lower Bound / Upper Bound

Mentioned Problems

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

 

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