Skip to content

Commit ae68f30

Browse files
add 478
1 parent cfd7b05 commit ae68f30

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,7 @@ _If you like this project, please leave me a star._ ★
681681
|480|[Sliding Window Median](https://leetcode.com/problems/sliding-window-median/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_480.java) | |Hard| Heap
682682
|479|[Largest Palindrome Product](https://leetcode.com/problems/largest-palindrome-product/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_479.java) | |Easy|
683683
|477|[Total Hamming Distance](https://leetcode.com/problems/total-hamming-distance/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_477.java) | |Medium| Bit Manipulation
684+
|478|[Generate Random Point in a Circle](https://leetcode.com/problems/generate-random-point-in-a-circle/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_478.java) | | Medium| Math, Random, Rejection Sampling
684685
|476|[Number Complement](https://leetcode.com/problems/number-complement/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_476.java) | | Easy| Bit Manipulation
685686
|475|[Heaters](https://leetcode.com/problems/heaters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_475.java) | |Easy | Array Binary Search
686687
|474|[Ones and Zeroes](https://leetcode.com/problems/ones-and-zeroes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_474.java) | |Medium| DP
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _478 {
4+
public static class Solution1 {
5+
/**
6+
* credit: https://leetcode.com/problems/generate-random-point-in-a-circle/discuss/154037/Polar-Coordinates-10-lines
7+
* and
8+
* https://leetcode.com/problems/generate-random-point-in-a-circle/discuss/155650/Explanation-with-Graphs-why-using-Math.sqrt()
9+
*/
10+
double radius, xCenter, yCenter;
11+
12+
public Solution1(double radius, double xCenter, double yCenter) {
13+
this.radius = radius;
14+
this.xCenter = xCenter;
15+
this.yCenter = yCenter;
16+
}
17+
18+
public double[] randPoint() {
19+
double len = Math.sqrt(Math.random()) * radius;
20+
double degree = Math.random() * 2 * Math.PI;
21+
double x = xCenter + len * Math.cos(degree);
22+
double y = yCenter + len * Math.sin(degree);
23+
return new double[]{x, y};
24+
}
25+
}
26+
27+
public static class Solution2 {
28+
/**
29+
* How to know one point is within a circle:
30+
* https://www.geeksforgeeks.org/find-if-a-point-lies-inside-or-on-circle/
31+
*/
32+
double top;
33+
double bottom;
34+
double left;
35+
double right;
36+
double rad;
37+
double xCenter;
38+
double yCenter;
39+
40+
public Solution2(double radius, double xCenter, double yCenter) {
41+
this.rad = radius;
42+
this.top = yCenter + radius;
43+
this.bottom = yCenter - radius;
44+
this.left = xCenter - radius;
45+
this.right = xCenter + radius;
46+
this.xCenter = xCenter;
47+
this.yCenter = yCenter;
48+
}
49+
50+
public double[] randPoint() {
51+
double[] result = new double[2];
52+
result[0] = (Math.random() * (right - left)) + left;
53+
result[1] = (Math.random() * (top - bottom)) + bottom;
54+
while (Math.pow(result[0] - xCenter, 2.0) + Math.pow(result[1] - yCenter, 2.0) > Math.pow(rad, 2.0)) {
55+
result[0] = (Math.random() * (right - left)) + left;
56+
result[1] = (Math.random() * (top - bottom)) + bottom;
57+
}
58+
return result;
59+
}
60+
}
61+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions._478;
5+
import org.junit.Test;
6+
7+
public class _478Test {
8+
private static _478.Solution1 solution1;
9+
10+
@Test
11+
public void test1() {
12+
solution1 = new _478.Solution1(10.0, 5.0, -7.5);
13+
CommonUtils.printArray(solution1.randPoint());
14+
}
15+
16+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy