Skip to content

Commit a9fd697

Browse files
add 1175
1 parent b59cb73 commit a9fd697

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ _If you like this project, please leave me a star._ ★
235235
|1182|[Shortest Distance to Target Color](https://leetcode.com/problems/shortest-distance-to-target-color/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1182.java) ||Medium|Binary Search|
236236
|1180|[Count Substrings with Only One Distinct Letter](https://leetcode.com/problems/count-substrings-with-only-one-distinct-letter/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1180.java) | |Easy|Math, String|
237237
|1176|[Diet Plan Performance](https://leetcode.com/problems/diet-plan-performance/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1176.java) | |Easy|Array, Sliding Window|
238-
|1175|[Prime Arrangements](https://leetcode.com/problems/prime-arrangements/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1175.java) | |Easy||
238+
|1175|[Prime Arrangements](https://leetcode.com/problems/prime-arrangements/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1175.java) | |Easy|Math|
239239
|1165|[Single-Row Keyboard](https://leetcode.com/problems/single-row-keyboard/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1165.java) | |Easy||
240240
|1161|[Maximum Level Sum of a Binary Tree](https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1161.java) | |Medium|Graph|
241241
|1160|[Find Words That Can Be Formed by Characters](https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1160.java)| |Easy||
Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,46 @@
11
package com.fishercoder.solutions;
22

3+
import java.math.BigInteger;
4+
import java.util.Arrays;
5+
36
public class _1175 {
47
public static class Solution1 {
5-
public int numPrimeArrangements(int n) {
6-
//TODO: implement it
7-
return -1;
8+
//credit: https://leetcode.com/problems/prime-arrangements/discuss/371884/Simple-Java-With-comment-sieve_of_eratosthenes
9+
static int MOD = 1000000007;
10+
11+
public static int numPrimeArrangements(int n) {
12+
int numberOfPrimes = generatePrimes(n);
13+
BigInteger x = factorial(numberOfPrimes);
14+
BigInteger y = factorial(n - numberOfPrimes);
15+
return x.multiply(y).mod(BigInteger.valueOf(MOD)).intValue();
16+
}
17+
18+
public static BigInteger factorial(int n) {
19+
BigInteger fac = BigInteger.ONE;
20+
for (int i = 2; i <= n; i++) {
21+
fac = fac.multiply(BigInteger.valueOf(i));
22+
}
23+
return fac.mod(BigInteger.valueOf(MOD));
24+
}
25+
26+
public static int generatePrimes(int n) {
27+
boolean[] prime = new boolean[n + 1];
28+
Arrays.fill(prime, 2, n + 1, true);
29+
for (int i = 2; i * i <= n; i++) {
30+
if (prime[i]) {
31+
for (int j = i * i; j <= n; j += i) {
32+
prime[j] = false;
33+
}
34+
}
35+
}
36+
int count = 0;
37+
for (int i = 0; i < prime.length; i++) {
38+
if (prime[i]) {
39+
count++;
40+
}
41+
}
42+
43+
return count;
844
}
945
}
1046
}

0 commit comments

Comments
 (0)