Count Complete Subarrays in an Array | Leetcode 2799

preview_player
Показать описание
This video explains Count Complete Subarrays in an Array using the most optimal sliding window two pointer approach.
----------------------------------------------------------------------------------------------------------------------------------------------------------------
🟣 JOIN our 𝐋𝐈𝐕𝐄 𝐢𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐭𝐫𝐚𝐢𝐧𝐢𝐧𝐠 𝐩𝐫𝐨𝐠𝐫𝐚𝐦 through whatsapp query: +91 8918633037
---------------------------------------------------------------------------------------------------------------------------------------------------------------

Рекомендации по теме
Комментарии
Автор

Wonderful explanation sir. I am able to understand the logic easily.

chandramoulyv
Автор

Brute force approach was able to pass 1007 test cases :)
Thank you for better approach.

sailendrachettri
Автор

This felt pretty easy, especially after the question asked a few days ago called "Count Good Subarrays". At first I used a set to count distinct elements, and a hashmap to traverse the window.. But since the value of nums[i] was pretty small, it was just easier and faster to take 2 arrays of size 2001.

class Solution {
public int countCompleteSubarrays(int[] arr) {
int[] set = new int[2001], temp = new int[2001];;
int dist = 0, left = 0, ans = 0, count = 0;
for(int i : arr){
set[i]++;
dist += set[i] == 1 ? 1 : 0;
}
for(int i = 0; i < arr.length; ++i){
temp[arr[i]]++;
count += temp[arr[i]] == 1 ? 1 : 0;
while(left <= i && count == dist){
ans += arr.length - i;
temp[arr[left]]--;
if(temp[arr[left]] == 0)
count--;
left++;
}
}
return ans;
}
}

AJ-jqhm
Автор

the new leetcode ui is such a disappointment according to me . What is your thought on it?

Anikait-hd
welcome to shbcf.ru