-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2638-count-the-number-of-k-free-subsets.js
More file actions
58 lines (48 loc) · 1.34 KB
/
2638-count-the-number-of-k-free-subsets.js
File metadata and controls
58 lines (48 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* 2638. Count the Number of K-Free Subsets
* https://leetcode.com/problems/count-the-number-of-k-free-subsets/
* Difficulty: Medium
*
* You are given an integer array nums, which contains distinct elements and an integer k.
*
* A subset is called a k-Free subset if it contains no two elements with an absolute
* difference equal to k. Notice that the empty set is a k-Free subset.
*
* Return the number of k-Free subsets of nums.
*
* A subset of an array is a selection of elements (possibly none) of the array.
*/
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var countTheNumOfKFreeSubsets = function(nums, k) {
nums.sort((a, b) => a - b);
const visited = new Set();
let result = 1;
for (const num of nums) {
if (visited.has(num)) continue;
const chain = [];
let current = num;
while (nums.includes(current) && !visited.has(current)) {
chain.push(current);
visited.add(current);
current += k;
}
result *= calculateChainSubsets(chain.length);
}
return result;
function calculateChainSubsets(length) {
if (length === 0) return 1;
if (length === 1) return 2;
let prev2 = 1;
let prev1 = 2;
for (let i = 2; i <= length; i++) {
const current = prev1 + prev2;
prev2 = prev1;
prev1 = current;
}
return prev1;
}
};