Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 104ae4e

Browse files
committed
Add solution #1735
1 parent 3973adc commit 104ae4e

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-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,508 LeetCode solutions in JavaScript
1+
# 1,509 LeetCode solutions in JavaScript
22

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

@@ -1336,6 +1336,7 @@
13361336
1732|[Find the Highest Altitude](./solutions/1732-find-the-highest-altitude.js)|Easy|
13371337
1733|[Minimum Number of People to Teach](./solutions/1733-minimum-number-of-people-to-teach.js)|Medium|
13381338
1734|[Decode XORed Permutation](./solutions/1734-decode-xored-permutation.js)|Medium|
1339+
1735|[Count Ways to Make Array With Product](./solutions/1735-count-ways-to-make-array-with-product.js)|Hard|
13391340
1748|[Sum of Unique Elements](./solutions/1748-sum-of-unique-elements.js)|Easy|
13401341
1749|[Maximum Absolute Sum of Any Subarray](./solutions/1749-maximum-absolute-sum-of-any-subarray.js)|Medium|
13411342
1752|[Check if Array Is Sorted and Rotated](./solutions/1752-check-if-array-is-sorted-and-rotated.js)|Easy|
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* 1735. Count Ways to Make Array With Product
3+
* https://leetcode.com/problems/count-ways-to-make-array-with-product/
4+
* Difficulty: Hard
5+
*
6+
* You are given a 2D integer array, queries. For each queries[i], where queries[i] = [ni, ki],
7+
* find the number of different ways you can place positive integers into an array of size ni
8+
* such that the product of the integers is ki. As the number of ways may be too large, the
9+
* answer to the ith query is the number of ways modulo 109 + 7.
10+
*
11+
* Return an integer array answer where answer.length == queries.length, and answer[i] is the
12+
* answer to the ith query.
13+
*/
14+
15+
/**
16+
* @param {number[][]} queries
17+
* @return {number[]}
18+
*/
19+
var waysToFillArray = function(queries) {
20+
const MOD = 1000000007n;
21+
const MAX_N = 20000;
22+
const factorial = new Array(MAX_N + 1).fill(1n);
23+
const inverse = new Array(MAX_N + 1).fill(1n);
24+
25+
for (let i = 1; i <= MAX_N; i++) {
26+
factorial[i] = (factorial[i - 1] * BigInt(i)) % MOD;
27+
}
28+
inverse[MAX_N] = BigInt(modInverse(Number(factorial[MAX_N] % MOD), Number(MOD)));
29+
for (let i = MAX_N - 1; i >= 0; i--) {
30+
inverse[i] = (inverse[i + 1] * BigInt(i + 1)) % MOD;
31+
}
32+
33+
const result = [];
34+
for (const [size, product] of queries) {
35+
if (product === 1) {
36+
result.push(1);
37+
continue;
38+
}
39+
const factors = getPrimeFactors(product);
40+
let ways = 1n;
41+
for (const count of factors.values()) {
42+
const n = size + count - 1;
43+
ways = (ways * BigInt(combinations(n, count))) % MOD;
44+
}
45+
result.push(Number(ways));
46+
}
47+
48+
return result;
49+
50+
function modInverse(a, m) {
51+
const m0 = m;
52+
let t;
53+
let q;
54+
let x0 = 0;
55+
let x1 = 1;
56+
while (a > 1) {
57+
q = Math.floor(a / m);
58+
t = m;
59+
m = a % m;
60+
a = t;
61+
t = x0;
62+
x0 = x1 - q * x0;
63+
x1 = t;
64+
}
65+
return x1 < 0 ? x1 + m0 : x1;
66+
}
67+
68+
function combinations(n, k) {
69+
if (k < 0 || k > n || n < 0 || n > MAX_N || n - k < 0) return 0;
70+
const result = (factorial[n] * inverse[k] * inverse[n - k]) % MOD;
71+
return Number(result);
72+
}
73+
74+
function getPrimeFactors(num) {
75+
const factors = new Map();
76+
for (let i = 2; i * i <= num; i++) {
77+
while (num % i === 0) {
78+
factors.set(i, (factors.get(i) || 0) + 1);
79+
num /= i;
80+
}
81+
}
82+
if (num > 1) {
83+
factors.set(num, (factors.get(num) || 0) + 1);
84+
}
85+
return factors;
86+
}
87+
};

0 commit comments

Comments
 (0)