-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path1852-distinct-numbers-in-each-subarray.js
More file actions
43 lines (35 loc) · 1.06 KB
/
1852-distinct-numbers-in-each-subarray.js
File metadata and controls
43 lines (35 loc) · 1.06 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
/**
* 1852. Distinct Numbers in Each Subarray
* https://leetcode.com/problems/distinct-numbers-in-each-subarray/
* Difficulty: Medium
*
* You are given an integer array nums of length n and an integer k. Your task is to find
* the number of distinct elements in every subarray of size k within nums.
*
* Return an array ans such that ans[i] is the count of distinct elements in
* nums[i..(i + k - 1)] for each index 0 <= i < n - k.
*/
/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/
var distinctNumbers = function(nums, k) {
const result = [];
const map = new Map();
for (let i = 0; i < k; i++) {
map.set(nums[i], (map.get(nums[i]) || 0) + 1);
}
result.push(map.size);
for (let i = k; i < nums.length; i++) {
const leftElement = nums[i - k];
const rightElement = nums[i];
map.set(leftElement, map.get(leftElement) - 1);
if (map.get(leftElement) === 0) {
map.delete(leftElement);
}
map.set(rightElement, (map.get(rightElement) || 0) + 1);
result.push(map.size);
}
return result;
};