Press "Enter" to skip to content

Posts tagged as “math”

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

Solution: Math

Time complexity: O(49/40) = O(1)

Time complexity: O(7/6 + 7 / 5) = O(1)

 

花花酱 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 840. Magic Squares In Grid

Problem

A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbersĀ from 1 to 9Ā such that each row, column, and both diagonals all have the same sum.

Given anĀ gridĀ of integers, how many 3 x 3 “magic square” subgrids are there?Ā  (Each subgrid is contiguous).

Example 1:

Input: [[4,3,8,4],
        [9,5,1,9],
        [2,7,6,2]]
Output: 1
Explanation: 
The following subgrid is a 3 x 3 magic square:
438
951
276

while this one is not:
384
519
762

In total, there is only one magic square inside the given grid.

Note:

  1. 1 <= grid.lengthĀ <= 10
  2. 1 <= grid[0].lengthĀ <= 10
  3. 0 <= grid[i][j] <= 15

Solution

Time complexity: O(m*n)

Space complexity: O(1)

C++