L10. Count number of Nice subarrays | 2 Pointers and Sliding Window Playlist
HTML-код
- Опубликовано: 25 мар 2024
- Notes/Codes/Problem links under step 10 of A2Z DSA Course: takeuforward.org/strivers-a2z...
Entire playlist: • Two Pointer and Slidin...
Follow us on our other social media handles: linktr.ee/takeuforward
i have been watching your video since 2 week, now I solved this without watching your video, whole credit goes to you
The way u change this problem to the previous question.. amazing. Thanks a lot😀💯
Thankyou so much Striver for all you efforts throughout in delivering us so much valuable content. Any student / working professional can now be able to transition their career without paying money for courses.
Would also like your insights on the point :
While preparing for interviews most of the aspirants are going through the videos solely and solving the question after completely watching the video. And also are feeling lazy trying to solve the question on our own. What is the best way to complete any topic without being lazy and how should an aspirant approach any topic/playlist?
this is the best channel
add this to your title - 1248. leetcode question number , such that it will come on top search .
There is a type the inner while loop should check while(map.size() > k ) then only we need to reduce the frequency
very good striver
thanks for this wonderful video
Understood ❤
Mindblown
nice explaination
Do add Leetcode question number as well in video title for better search results.
understood
Striver on a different level
Finally mil gya 🥲
there is no need for modifying array to 0 and 1.
We can just use sliding window with concept of finding subarrays with odd numbers less the k - subarrays with odd numbers less then k-1
yeah tru dat, he just did so that to convert into the prev qs
striver we want strings and stack playlist
here we are using O(4N) TC at worst case but there's a better approach to solve it with [TC = O(N) & SC = O(1)]
solutions:
class Solution {
public:
int solve(vector& nums, int k){
if(k
❤
class Solution {
public int numberOfSubarrays(int[] nums, int k) {
int left = 0;
int right = 0;
int count = 0;
Queue q = new LinkedList();
int n = nums.length;
while(right < n)
{
if(nums[right] % 2 == 1)
{
q.add(right);
}
while(left < right && q.size() > k)
{
left = q.poll() + 1;
}
if(q.size() == k)
{
count += (q.peek() - left) + 1;
}
right++;
}
return count;
}
}
Dude, we think so alike :)
I didn't see anyone utilizing queues in this problem
class Solution:
def numberOfSubarrays(self, nums: List[int], k: int) -> int:
odd_queue = deque()
j, count = 0, 0
for i, num in enumerate(nums):
if num % 2 == 1:
odd_queue.append(i)
if len(odd_queue) > k:
j = odd_queue.popleft() + 1
if len(odd_queue) == k:
count += 1 + odd_queue[0] - j
return count
but it wont work for the test case:
[2,2,2,1,2,2,1,2,2,2] k=2
Because at l=0 r=0, r will reach upto last index because sum
It is not like that, instead we are counting number of possible substring at each index it is going, (count += (right-left+1)), for l=0, r=0 count = 1, l=0, r=1, count += 1-0+1 => count = 3, count = 6, ..... similarly it will reach the end(as k
1248. Leetcode problem
3:15 AM at night 🌃
public int numberOfSubarrays(int[]nums,int k){
int n=nums.length;
int[]cnt=new int[n+1];
cnt[0]=1;
int sum=0,ans=0;
for(int num:nums){
sum+=num%2;
if(sum>=k)
ans+=cnt[sum-k];
cnt[sum]++;
}
return ans;
}
🎉❤
class Solution {
public int helpMe(int[] nums,int goal){
int l=0;
int r=0;
int sum=0;
int cnt=0;
if(goal
I think it will fail for a=[2,4,6] and k=2
nhi bro maine khud solve kiya toh fail nhi hua bs func me
if(k
@@es_amit tum kitnai saal k ho?
@@Harsh-jc2bz 20 kyu kuch kaam hai?