Press "Enter" to skip to content

Posts published in “Math”

花花酱 LeetCode 470. Implement Rand10() Using Rand7()

Problem

Given a function rand7 which generates a uniform random integer in the range 1 to 7, write a function rand10 which generates a uniform random integer in the range 1 to 10.

Do NOT use system’s Math.random().

Example 1:

Input: 1
Output: [7]

Example 2:

Input: 2
Output: [8,4]

Example 3:

Input: 3
Output: [8,1,10]

Note:

  1. rand7 is predefined.
  2. Each testcase has one argument: n, the number of times that rand10 is called.

Follow up:

  1. What is the expected value for the number of calls to rand7() function?
  2. Could you minimize the number of calls to rand7()?

Solution:

Time complexity: O(1)

Space complexity: O(1)

C++

花花酱 LeetCode 923. 3Sum With Multiplicity

Problem

Given an integer array A, and an integer target, return the number of tuples i, j, k  such that i < j < k and A[i] + A[j] + A[k] == target.

As the answer can be very large, return it modulo 10^9 + 7.

Example 1:

Input: A = [1,1,2,2,3,3,4,4,5,5], target = 8
Output: 20
Explanation: 
Enumerating by the values (A[i], A[j], A[k]):
(1, 2, 5) occurs 8 times;
(1, 3, 4) occurs 8 times;
(2, 2, 4) occurs 2 times;
(2, 3, 3) occurs 2 times.

Example 2:

Input: A = [1,1,2,2,2,2], target = 5
Output: 12
Explanation: 
A[i] = 1, A[j] = A[k] = 2 occurs 12 times:
We choose one 1 from [1,1] in 2 ways,
and two 2s from [2,2,2,2] in 6 ways.

Note:

  1. 3 <= A.length <= 3000
  2. 0 <= A[i] <= 100
  3. 0 <= target <= 300

Solution: Math / Combination

Time complexity: O(n + |target|^2)

Space complexity: O(|target|)

C++

花花酱 LeetCode 914. X of a Kind in a Deck of Cards

Problem

n a deck of cards, each card has an integer written on it.

Return true if and only if you can choose X >= 2 such that it is possible to split the entire deck into 1 or more groups of cards, where:

  • Each group has exactly X cards.
  • All the cards in each group have the same integer.

Example 1:

Input: [1,2,3,4,4,3,2,1]
Output: true
Explanation: Possible partition [1,1],[2,2],[3,3],[4,4]

Example 2:

Input: [1,1,1,2,2,2,3,3]
Output: false
Explanation: No possible partition.

Example 3:

Input: [1]
Output: false
Explanation: No possible partition.

Example 4:

Input: [1,1]
Output: true
Explanation: Possible partition [1,1]

Example 5:

Input: [1,1,2,2,2,2]
Output: true
Explanation: Possible partition [1,1],[2,2],[2,2]

Note:

  1. 1 <= deck.length <= 10000
  2. 0 <= deck[i] < 10000

Solution 1: HashTable + Brute Force

Try all possible Xs. 2 ~ deck.size()

Time complexity: ~O(n)

Space complexity: O(1)

C++

Solution 2: HashTable + GCD

Time complexity: O(nlogn)

Space complexity: O(1)

C++

Java

 

 

花花酱 LeetCode 908. Smallest Range I

Problem

Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and add xto A[i].

After this process, we have some array B.

Return the smallest possible difference between the maximum value of B and the minimum value of B.

Example 1:

Input: A = [1], K = 0
Output: 0
Explanation: B = [1]

Example 2:

Input: A = [0,10], K = 2
Output: 6
Explanation: B = [2,8]

Example 3:

Input: A = [1,3,6], K = 3
Output: 0
Explanation: B = [3,3,3] or B = [4,4,4]

Note:

  1. 1 <= A.length <= 10000
  2. 0 <= A[i] <= 10000
  3. 0 <= K <= 10000

Solution 0: Brute Force (TLE)

Try all pairs

Time complexity: O(n^2)

Space complexity: O(1)

Solution 1: Math

Time complexity: O(n)

Space complexity: O(1)

Find the min/max element of the array.

min + k v.s. max – k

ans = max(0, (max – min) – 2 * k))

C++

Python3

花花酱 LeetCode 902. Numbers At Most N Given Digit Set

Problem

We have a sorted set of digits D, a non-empty subset of {'1','2','3','4','5','6','7','8','9'}.  (Note that '0' is not included.)

Now, we write numbers using these digits, using each digit as many times as we want.  For example, if D = {'1','3','5'}, we may write numbers such as '13', '551', '1351315'.

Return the number of positive integers that can be written (using the digits of D) that are less than or equal to N.

Example 1:

Input: D = ["1","3","5","7"], N = 100
Output: 20
Explanation: 
The 20 numbers that can be written are:
1, 3, 5, 7, 11, 13, 15, 17, 31, 33, 35, 37, 51, 53, 55, 57, 71, 73, 75, 77.

Example 2:

Input: D = ["1","4","9"], N = 1000000000
Output: 29523
Explanation: 
We can write 3 one digit numbers, 9 two digit numbers, 27 three digit numbers,
81 four digit numbers, 243 five digit numbers, 729 six digit numbers,
2187 seven digit numbers, 6561 eight digit numbers, and 19683 nine digit numbers.
In total, this is 29523 integers that can be written using the digits of D.

Note:

  1. D is a subset of digits '1'-'9' in sorted order.
  2. 1 <= N <= 10^9

 

Solution -1: DFS (TLE)

Time complexity: O(|D|^log10(N))

Space complexity: O(n)

Solution 1: Math

Time complexity: O(log10(N))

Space complexity: O(1)

Suppose N has n digits.

less than n digits

We can use all the numbers from D to construct numbers of with length 1,2,…,n-1 which are guaranteed to be less than N.

e.g. n = 52125, D = [1, 2, 5]

format X: e.g. 1, 2, 5 counts = |D| ^ 1

format XX: e.g. 11,12,15,21,22,25,51,52,55, counts = |D|^2

format XXX:  counts = |D|^3

format XXXX: counts = |D|^4

exact n digits

if all numbers in D  != N[0], counts = |d < N[0] | d in D| * |D|^(n-1), and we are done.

e.g. N = 34567, D = [1,2,8]

we can make:

  • X |3|^1
  • XX |3| ^ 2
  • XXX |3| ^ 3
  • XXXX |3| ^ 3
  • 1XXXX, |3|^4
  • 2XXXX, |3|^4
  • we can’t do 8XXXX

Total = (3^1 + 3^2 + 3^3 + 3^4) + 2 * |3|^ 4 = 120 + 162 = 282

N = 52525, D = [1,2,5]

However, if d = N[i], we need to check the next digit…

  • X |3|^1
  • XX |3| ^ 2
  • XXX |3| ^ 3
  • XXXX |3| ^ 3
  • 1XXXX, |3|^4
  • 2XXXX, |3|^4
  •  5????
    • 51XXX |3|^3
    • 52???
      • 521XX |3|^2
      • 522XX |3|^2
      • 525??
        • 5251X |3|^1
        • 5252?
          • 52521 |3|^0
          • 52522 |3|^0
          • 52525 +1

total = (120) + 2 * |3|^4 + |3|^3 + 2*|3|^2 + |3|^1 + 2 * |3|^0 + 1 = 120 + 213 = 333

if every digit of N is from D, then we also have a valid solution, thus need to + 1.

C++

Java

 

Python3