Skip to content

Commit 0411163

Browse files
authored
Added tasks 477, 478, 479, 480.
1 parent f953133 commit 0411163

File tree

13 files changed

+486
-142
lines changed

13 files changed

+486
-142
lines changed

README.md

+146-142
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g0401_0500.s0477_total_hamming_distance
2+
3+
// #Medium #Array #Math #Bit_Manipulation #2023_01_01_Time_298_ms_(100.00%)_Space_38.6_MB_(100.00%)
4+
5+
class Solution {
6+
fun totalHammingDistance(nums: IntArray): Int {
7+
var ans = 0
8+
val n = nums.size
9+
for (i in 0..31) {
10+
var ones = 0
11+
for (k in nums) {
12+
ones += k shr i and 1
13+
}
14+
ans = ans + ones * (n - ones)
15+
}
16+
return ans
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
477\. Total Hamming Distance
2+
3+
Medium
4+
5+
The [Hamming distance](https://en.wikipedia.org/wiki/Hamming_distance) between two integers is the number of positions at which the corresponding bits are different.
6+
7+
Given an integer array `nums`, return _the sum of **Hamming distances** between all the pairs of the integers in_ `nums`.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [4,14,2]
12+
13+
**Output:** 6
14+
15+
**Explanation:** In binary representation, the 4 is 0100, 14 is 1110, and 2 is 0010 (just showing the four bits relevant in this case).
16+
17+
The answer will be:
18+
19+
HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6.
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [4,14,4]
24+
25+
**Output:** 4
26+
27+
**Constraints:**
28+
29+
* <code>1 <= nums.length <= 10<sup>4</sup></code>
30+
* <code>0 <= nums[i] <= 10<sup>9</sup></code>
31+
* The answer for the given input will fit in a **32-bit** integer.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g0401_0500.s0478_generate_random_point_in_a_circle
2+
3+
// #Medium #Math #Geometry #Randomized #Rejection_Sampling
4+
// #2023_01_01_Time_862_ms_(100.00%)_Space_70_MB_(66.67%)
5+
6+
import java.util.Random
7+
8+
@Suppress("kotlin:S2245")
9+
class Solution(private val radius: Double, private val xCenter: Double, private val yCenter: Double) {
10+
private val random: Random = Random()
11+
fun randPoint(): DoubleArray {
12+
var x = getCoordinate(xCenter)
13+
var y = getCoordinate(yCenter)
14+
while (getDistance(x, y) >= radius * radius) {
15+
x = getCoordinate(xCenter)
16+
y = getCoordinate(yCenter)
17+
}
18+
return doubleArrayOf(x, y)
19+
}
20+
21+
private fun getDistance(x: Double, y: Double): Double {
22+
return (xCenter - x) * (xCenter - x) + (yCenter - y) * (yCenter - y)
23+
}
24+
25+
private fun getCoordinate(center: Double): Double {
26+
return center - radius + random.nextDouble() * 2 * radius
27+
}
28+
}
29+
30+
/*
31+
* Your Solution object will be instantiated and called as such:
32+
* var obj = Solution(radius, x_center, y_center)
33+
* var param_1 = obj.randPoint()
34+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
478\. Generate Random Point in a Circle
2+
3+
Medium
4+
5+
Given the radius and the position of the center of a circle, implement the function `randPoint` which generates a uniform random point inside the circle.
6+
7+
Implement the `Solution` class:
8+
9+
* `Solution(double radius, double x_center, double y_center)` initializes the object with the radius of the circle `radius` and the position of the center `(x_center, y_center)`.
10+
* `randPoint()` returns a random point inside the circle. A point on the circumference of the circle is considered to be in the circle. The answer is returned as an array `[x, y]`.
11+
12+
**Example 1:**
13+
14+
**Input** ["Solution", "randPoint", "randPoint", "randPoint"] [[1.0, 0.0, 0.0], [], [], []]
15+
16+
**Output:** [null, [-0.02493, -0.38077], [0.82314, 0.38945], [0.36572, 0.17248]]
17+
18+
**Explanation:**
19+
20+
Solution solution = new Solution(1.0, 0.0, 0.0);
21+
solution.randPoint(); // return [-0.02493, -0.38077]
22+
solution.randPoint(); // return [0.82314, 0.38945]
23+
solution.randPoint(); // return [0.36572, 0.17248]
24+
25+
**Constraints:**
26+
27+
* <code>0 < radius <= 10<sup>8</sup></code>
28+
* <code>-10<sup>7</sup> <= x_center, y_center <= 10<sup>7</sup></code>
29+
* At most <code>3 * 10<sup>4</sup></code> calls will be made to `randPoint`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g0401_0500.s0479_largest_palindrome_product
2+
3+
// #Hard #Math #2023_01_01_Time_147_ms_(100.00%)_Space_32.8_MB_(100.00%)
4+
5+
@Suppress("NAME_SHADOWING")
6+
class Solution {
7+
fun largestPalindrome(n: Int): Int {
8+
val pow10 = Math.pow(10.0, n.toDouble()).toLong()
9+
val max = (pow10 - 1) * (pow10 - Math.sqrt(pow10.toDouble()).toLong() + 1)
10+
val left = max / pow10
11+
var t = pow10 / 11
12+
t -= t.inv() and 1L
13+
for (i in left downTo 1) {
14+
var j = t
15+
val num = gen(i)
16+
while (j >= i / 11) {
17+
if (num % j == 0L) {
18+
return (num % 1337).toInt()
19+
}
20+
j -= 2
21+
}
22+
}
23+
return 9
24+
}
25+
26+
private fun gen(x: Long): Long {
27+
var x = x
28+
var r = x
29+
while (x > 0) {
30+
r = r * 10 + x % 10
31+
x /= 10
32+
}
33+
return r
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
479\. Largest Palindrome Product
2+
3+
Hard
4+
5+
Given an integer n, return _the **largest palindromic integer** that can be represented as the product of two `n`\-digits integers_. Since the answer can be very large, return it **modulo** `1337`.
6+
7+
**Example 1:**
8+
9+
**Input:** n = 2
10+
11+
**Output:** 987 Explanation: 99 x 91 = 9009, 9009 % 1337 = 987
12+
13+
**Example 2:**
14+
15+
**Input:** n = 1
16+
17+
**Output:** 9
18+
19+
**Constraints:**
20+
21+
* `1 <= n <= 8`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package g0401_0500.s0480_sliding_window_median
2+
3+
// #Hard #Array #Hash_Table #Heap_Priority_Queue #Sliding_Window
4+
// #2023_01_01_Time_409_ms_(100.00%)_Space_44.6_MB_(81.48%)
5+
6+
import java.util.TreeSet
7+
8+
class Solution {
9+
fun medianSlidingWindow(nums: IntArray, k: Int): DoubleArray {
10+
require(k >= 1) { "Input is invalid" }
11+
val len = nums.size
12+
val result = DoubleArray(len - k + 1)
13+
if (k == 1) {
14+
for (i in 0 until len) {
15+
result[i] = nums[i].toDouble()
16+
}
17+
return result
18+
}
19+
val comparator = Comparator { a: Int?, b: Int? ->
20+
if (nums[a!!] != nums[b!!]
21+
) Integer.compare(nums[a], nums[b]) else Integer.compare(a, b)
22+
}
23+
val smallNums = TreeSet(comparator.reversed())
24+
val largeNums = TreeSet(comparator)
25+
for (i in 0 until len) {
26+
if (i >= k) {
27+
removeElement(smallNums, largeNums, i - k)
28+
}
29+
addElement(smallNums, largeNums, i)
30+
if (i >= k - 1) {
31+
result[i - (k - 1)] = getMedian(smallNums, largeNums, nums)
32+
}
33+
}
34+
return result
35+
}
36+
37+
private fun addElement(smallNums: TreeSet<Int?>, largeNums: TreeSet<Int?>, idx: Int) {
38+
smallNums.add(idx)
39+
largeNums.add(smallNums.pollFirst()!!)
40+
if (smallNums.size < largeNums.size) {
41+
smallNums.add(largeNums.pollFirst())
42+
}
43+
}
44+
45+
private fun removeElement(smallNums: TreeSet<Int?>, largeNums: TreeSet<Int?>, idx: Int) {
46+
if (largeNums.contains(idx)) {
47+
largeNums.remove(idx)
48+
if (smallNums.size == largeNums.size + 2) {
49+
largeNums.add(smallNums.pollFirst()!!)
50+
}
51+
} else {
52+
smallNums.remove(idx)
53+
if (smallNums.size < largeNums.size) {
54+
smallNums.add(largeNums.pollFirst())
55+
}
56+
}
57+
}
58+
59+
private fun getMedian(smallNums: TreeSet<Int?>, largeNums: TreeSet<Int?>, nums: IntArray): Double {
60+
return if (smallNums.size == largeNums.size) {
61+
(nums[smallNums.first()!!].toDouble() + nums[largeNums.first()!!]) / 2
62+
} else nums[smallNums.first()!!].toDouble()
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
480\. Sliding Window Median
2+
3+
Hard
4+
5+
The **median** is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.
6+
7+
* For examples, if <code>arr = [2,<ins>3</ins>,4]</code>, the median is `3`.
8+
* For examples, if <code>arr = [1,<ins>2,3</ins>,4]</code>, the median is `(2 + 3) / 2 = 2.5`.
9+
10+
You are given an integer array `nums` and an integer `k`. There is a sliding window of size `k` which is moving from the very left of the array to the very right. You can only see the `k` numbers in the window. Each time the sliding window moves right by one position.
11+
12+
Return _the median array for each window in the original array_. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [1,3,-1,-3,5,3,6,7], k = 3
17+
18+
**Output:** [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000]
19+
20+
**Explanation:** Window position Median --------------- ----- [**1 3 -1**] -3 5 3 6 7 1 1 [**3 -1 -3**] 5 3 6 7 -1 1 3 [**\-1 -3 5**] 3 6 7 -1 1 3 -1 [**\-3 5 3**] 6 7 3 1 3 -1 -3 [**5 3 6**] 7 5 1 3 -1 -3 5 [**3 6 7**] 6
21+
22+
**Example 2:**
23+
24+
**Input:** nums = [1,2,3,4,2,3,1,4,2], k = 3
25+
26+
**Output:** [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000]
27+
28+
**Constraints:**
29+
30+
* <code>1 <= k <= nums.length <= 10<sup>5</sup></code>
31+
* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0401_0500.s0477_total_hamming_distance
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun totalHammingDistance() {
10+
assertThat(Solution().totalHammingDistance(intArrayOf(4, 14, 2)), equalTo(6))
11+
}
12+
13+
@Test
14+
fun totalHammingDistance2() {
15+
assertThat(Solution().totalHammingDistance(intArrayOf(4, 14, 4)), equalTo(4))
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package g0401_0500.s0478_generate_random_point_in_a_circle
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun randPoint() {
10+
val solution = Solution(1.0, 0.0, 0.0)
11+
solution.randPoint()
12+
solution.randPoint()
13+
solution.randPoint()
14+
assertThat(solution, equalTo(solution))
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0401_0500.s0479_largest_palindrome_product
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun largestPalindrome() {
10+
assertThat(Solution().largestPalindrome(2), equalTo(987))
11+
}
12+
13+
@Test
14+
fun largestPalindrome2() {
15+
assertThat(Solution().largestPalindrome(1), equalTo(9))
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g0401_0500.s0480_sliding_window_median
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun medianSlidingWindow() {
10+
assertThat(
11+
Solution().medianSlidingWindow(intArrayOf(1, 3, -1, -3, 5, 3, 6, 7), 3),
12+
equalTo(doubleArrayOf(1.00000, -1.00000, -1.00000, 3.00000, 5.00000, 6.00000))
13+
)
14+
}
15+
16+
@Test
17+
fun medianSlidingWindow2() {
18+
assertThat(
19+
Solution().medianSlidingWindow(intArrayOf(1, 2, 3, 4, 2, 3, 1, 4, 2), 3),
20+
equalTo(
21+
doubleArrayOf(
22+
2.00000, 3.00000, 3.00000, 3.00000, 2.00000, 3.00000, 2.00000
23+
)
24+
)
25+
)
26+
}
27+
}

0 commit comments

Comments
 (0)