File tree 2 files changed +49
-1
lines changed
2 files changed +49
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,166 LeetCode solutions in JavaScript
1
+ # 1,167 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
914
914
1170|[ Compare Strings by Frequency of the Smallest Character] ( ./solutions/1170-compare-strings-by-frequency-of-the-smallest-character.js ) |Medium|
915
915
1171|[ Remove Zero Sum Consecutive Nodes from Linked List] ( ./solutions/1171-remove-zero-sum-consecutive-nodes-from-linked-list.js ) |Medium|
916
916
1172|[ Dinner Plate Stacks] ( ./solutions/1172-dinner-plate-stacks.js ) |Hard|
917
+ 1175|[ Prime Arrangements] ( ./solutions/1175-prime-arrangements.js ) |Easy|
917
918
1189|[ Maximum Number of Balloons] ( ./solutions/1189-maximum-number-of-balloons.js ) |Easy|
918
919
1200|[ Minimum Absolute Difference] ( ./solutions/1200-minimum-absolute-difference.js ) |Easy|
919
920
1206|[ Design Skiplist] ( ./solutions/1206-design-skiplist.js ) |Hard|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments