Press "Enter" to skip to content

Posts published in “Math”

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

花花酱 LeetCode 43. Multiply Strings

Problem

Given two non-negative integersĀ num1Ā andĀ num2Ā represented as strings, return the product ofĀ num1Ā andĀ num2, also represented as a string.

Example 1:

Input: num1 = "2", num2 = "3"
Output: "6"

Example 2:

Input: num1 = "123", num2 = "456"
Output: "56088"

Note:

  1. The length of bothĀ num1Ā andĀ num2Ā is < 110.
  2. BothĀ num1Ā andĀ num2Ā containĀ only digitsĀ 0-9.
  3. BothĀ num1Ā andĀ num2Ā do not contain any leading zero, except the number 0 itself.
  4. YouĀ must not use any built-in BigInteger libraryĀ orĀ convert the inputs to integerĀ directly.

Solution: Simulation

Simulate multiplication one digit at a time.

Time complexity: O(l1*l2)

Space complexity: O(l1 + l2)

C++

 

花花酱 LeetCode 357. Count Numbers with Unique Digits

Problem

Given aĀ non-negativeĀ integer n, count all numbers with unique digits, x, where 0 ā‰¤ x < 10n.

Example:
Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ā‰¤ x < 100, excludingĀ [11,22,33,44,55,66,77,88,99])

Solution: Math

f(0) = 1 (0)

f(1) = 10 (0 – 9)

f(2) = 9 * 9 (1-9 * (0 ~ 9 exclude the one from first digit))

f(3) = 9 * 9 * 8

f(4) = 9 * 9 * 8 * 7

f(x) = 0 if x >= 10

ans = sum(f[1] ~ f[n])

Time complexity: O(1)

Space complexity: O(1)

 

花花酱 LeetCode 867. Prime Palindrome

Problem

Find the smallest prime palindrome greater than or equal toĀ N.

Recall that aĀ number isĀ primeĀ if it’s only divisors are 1 and itself, and it is greater than 1.

For example, 2,3,5,7,11 and 13 areĀ primes.

Recall that a number is aĀ palindromeĀ if it reads the same from left to right as it does from right to left.

For example, 12321 is a palindrome.

 

Example 1:

Input: 6
Output: 7

Example 2:

Input: 8
Output: 11

Example 3:

Input: 13
Output: 101

Note:

  • 1 <= N <= 10^8
  • The answer is guaranteed to exist and be less thanĀ 2 * 10^8.

Solution: Math

All odd digits palindromes have a factor 11, they are not prime except 11 itself.

Time complexity: O(n)

Space complexity: O(1)