Skip to content

Commit de922cc

Browse files
committedApr 5, 2025
Add solution #1175
1 parent 42f75a8 commit de922cc

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,166 LeetCode solutions in JavaScript
1+
# 1,167 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -914,6 +914,7 @@
914914
1170|[Compare Strings by Frequency of the Smallest Character](./solutions/1170-compare-strings-by-frequency-of-the-smallest-character.js)|Medium|
915915
1171|[Remove Zero Sum Consecutive Nodes from Linked List](./solutions/1171-remove-zero-sum-consecutive-nodes-from-linked-list.js)|Medium|
916916
1172|[Dinner Plate Stacks](./solutions/1172-dinner-plate-stacks.js)|Hard|
917+
1175|[Prime Arrangements](./solutions/1175-prime-arrangements.js)|Easy|
917918
1189|[Maximum Number of Balloons](./solutions/1189-maximum-number-of-balloons.js)|Easy|
918919
1200|[Minimum Absolute Difference](./solutions/1200-minimum-absolute-difference.js)|Easy|
919920
1206|[Design Skiplist](./solutions/1206-design-skiplist.js)|Hard|

‎solutions/1175-prime-arrangements.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* 1175. Prime Arrangements
3+
* https://leetcode.com/problems/prime-arrangements/
4+
* Difficulty: Easy
5+
*
6+
* Return the number of permutations of 1 to n so that prime numbers are at prime
7+
* indices (1-indexed.)
8+
*
9+
* (Recall that an integer is prime if and only if it is greater than 1, and cannot be written
10+
* as a product of two positive integers both smaller than it.)
11+
*
12+
* Since the answer may be large, return the answer modulo 10^9 + 7.
13+
*/
14+
15+
/**
16+
* @param {number} n
17+
* @return {number}
18+
*/
19+
var numPrimeArrangements = function(n) {
20+
let primeCount = 0;
21+
for (let i = 1; i <= n; i++) {
22+
if (isPrime(i)) primeCount++;
23+
}
24+
25+
const MOD = 1000000007n;
26+
const nonPrimeCount = n - primeCount;
27+
const primePermutations = factorial(primeCount);
28+
const nonPrimePermutations = factorial(nonPrimeCount);
29+
30+
return Number((primePermutations * nonPrimePermutations) % MOD);
31+
32+
function isPrime(n) {
33+
if (n < 2) return false;
34+
for (let i = 2; i * i <= n; i++) {
35+
if (n % i === 0) return false;
36+
}
37+
return true;
38+
}
39+
40+
function factorial(n) {
41+
let product = 1n;
42+
for (let i = 2; i <= n; i++) {
43+
product *= BigInt(i);
44+
}
45+
return product;
46+
}
47+
};

0 commit comments

Comments
 (0)
Please sign in to comment.