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

Commit 172777e

Browse files
committed
Add solution #1994
1 parent f076412 commit 172777e

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-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,675 LeetCode solutions in JavaScript
1+
# 1,676 LeetCode solutions in JavaScript
22

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

@@ -1529,6 +1529,7 @@
15291529
1991|[Find the Middle Index in Array](./solutions/1991-find-the-middle-index-in-array.js)|Easy|
15301530
1992|[Find All Groups of Farmland](./solutions/1992-find-all-groups-of-farmland.js)|Medium|
15311531
1993|[Operations on Tree](./solutions/1993-operations-on-tree.js)|Medium|
1532+
1994|[The Number of Good Subsets](./solutions/1994-the-number-of-good-subsets.js)|Hard|
15321533
1996|[The Number of Weak Characters in the Game](./solutions/1996-the-number-of-weak-characters-in-the-game.js)|Medium|
15331534
2000|[Reverse Prefix of Word](./solutions/2000-reverse-prefix-of-word.js)|Easy|
15341535
2011|[Final Value of Variable After Performing Operations](./solutions/2011-final-value-of-variable-after-performing-operations.js)|Easy|
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* 1994. The Number of Good Subsets
3+
* https://leetcode.com/problems/the-number-of-good-subsets/
4+
* Difficulty: Hard
5+
*
6+
* You are given an integer array nums. We call a subset of nums good if its product can be
7+
* represented as a product of one or more distinct prime numbers.
8+
*
9+
* - For example, if nums = [1, 2, 3, 4]:
10+
* - [2, 3], [1, 2, 3], and [1, 3] are good subsets with products 6 = 2*3, 6 = 2*3, and 3 = 3
11+
* respectively.
12+
* - [1, 4] and [4] are not good subsets with products 4 = 2*2 and 4 = 2*2 respectively.
13+
*
14+
* Return the number of different good subsets in nums modulo 109 + 7.
15+
*
16+
* A subset of nums is any array that can be obtained by deleting some (possibly none or all)
17+
* elements from nums. Two subsets are different if and only if the chosen indices to delete
18+
* are different.
19+
*/
20+
21+
/**
22+
* @param {number[]} nums
23+
* @return {number}
24+
*/
25+
var numberOfGoodSubsets = function(nums) {
26+
const MOD = 1e9 + 7;
27+
const primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29];
28+
const freq = new Array(31).fill(0);
29+
for (const num of nums) {
30+
freq[num]++;
31+
}
32+
33+
const dp = new Array(1 << primes.length).fill(0);
34+
dp[0] = 1;
35+
for (let i = 0; i < freq[1]; i++) {
36+
dp[0] = (dp[0] * 2) % MOD;
37+
}
38+
39+
for (let num = 2; num <= 30; num++) {
40+
if (freq[num] === 0) continue;
41+
let mask = 0;
42+
let valid = true;
43+
for (let i = 0; i < primes.length; i++) {
44+
let count = 0;
45+
let temp = num;
46+
while (temp % primes[i] === 0) {
47+
count++;
48+
temp /= primes[i];
49+
}
50+
if (count > 1) {
51+
valid = false;
52+
break;
53+
}
54+
if (count === 1) {
55+
mask |= 1 << i;
56+
}
57+
}
58+
59+
if (!valid) continue;
60+
61+
const prev = dp.slice();
62+
for (let j = 0; j < 1 << primes.length; j++) {
63+
if ((j & mask) === 0) {
64+
dp[j | mask] = (dp[j | mask] + prev[j] * freq[num]) % MOD;
65+
}
66+
}
67+
}
68+
69+
let result = 0;
70+
for (let i = 1; i < 1 << primes.length; i++) {
71+
result = (result + dp[i]) % MOD;
72+
}
73+
74+
return result;
75+
};

0 commit comments

Comments
 (0)