In this tutorial, I have explained how to count distinct elements in every window of size k and it's code.
Given an array of n integers and an integer k (k is always smaller or equal to n). Return the count of distinct elements in all windows (or in all sub-arrays) of size k.
webrewrite.com...
For example -
Example 1:
Input: {1, 5, 9, 3, 3, 7, 3}, k = 3
Output: {3, 3, 2, 2, 2}
1st window {1, 5, 9}, Distinct elements are 3
2nd window {5, 9, 3}, Distinct elements are 3
3rd window {9, 3, 3}, Distinct elements are 2
4th window {3, 3, 7}, Distinct elements are 2
5th window {3, 7, 3}, Distinct elements are 2
Example 2:
Input: {1, 4, 7, 7}, k = 2
Output: {2, 2, 1}
1st window {1, 4}, Distinct elements are 2
2nd window {4, 7}, Distinct elements are 2
3rd window {7, 7}, Distinct elements are 1
Website - webrewrite.com/
Paypal - www.paypal.me/...
Негізгі бет Count Distinct Elements in Every Window of Size K
Пікірлер: 15