Have added Timestamps for anyone just here to solve the problem, just watch starting 14mins 🌚, for others watch depending on jitni himmaat bachi ho 💀 . Sub nhi bharvaye naa aap logo ne 🥺
I tried it with two ordered maps and the max value i was storing with -nums[j] trick because the if nums[j] is maximum in window then-nums[j] will be least but unfortunately it failed , some runtime error was coming and the code was logically correct and also that condition if mp[nums[j]] == 0 so do mp.erase(nums[j]) but also the runtime error was coming and also when keys are negative, in the max ordered map , i was not able to access the key with this method mp[nums[j]] because -ve indices not exist so i used mp.at(nums[j]) but it was failing
I have seen a lot of comments on people struggling with Min and Max heap including me,🥲 Like this comment you want Aryan bhaiya to make a video on the basics and in dept analysis of priority queue(MIN heap and MAX heap) , how to use them as it is a lot confusing.
intuition - intialize a flag = 1 and if nums[i] is !flag (i.e. 0), increase the count and toggle the flag. So the main idea is to toggle the flag whenever we encounter the alternate value. We don't need to change the array manually, just need to create it hypothetically. class Solution { public int minOperations(int[] nums) { int ans = 0; int flag = 1; for(int i = 0; i < nums.length; i++) { if(nums[i] != flag) { ans++; flag = 1 - flag; } } return ans; } }
this will not work bro , imagine your min element was at starting of your subarray and next min is at 2nd last of subarray , so you will have to iterate whole subarray to know which is next min or max element
@@codebreaker6578 class Solution { public: int longestSubarray(vector& nums, int limit) { int min_el = nums[0]; int min_idx = 0; int max_el = nums[0]; int max_idx = 0; int n = nums.size(); int ans = 1; // minimum subarray size int left = 0; // left pointer int right = 0; // right pointer while (right < n) { // Update min and max elements and their indices if (min_el >= nums[right]) { min_idx = right; min_el = nums[right]; } if (max_el limit) { // Move left pointer to right of min_idx or max_idx left = min(min_idx, max_idx) + 1; // Update min_el and max_el to the new values in the window min_el = nums[left]; max_el = nums[left]; // Find new min_idx and max_idx in the new window for (int i = left; i = nums[i]) { min_idx = i; min_el = nums[i]; } if (max_el
When the condition violates, it is either due to min or max, so we need to update or new min or max at right index and then based on weather it is min or max, find second min or second max from the existing window.
Very informative video, Thanks bro, you explained it very clearly in all approaches. Loved your explaination. I was thinking of heaps before solving but was unable to achieve the logic while shrinking and deleting. Multiset was never in mind🙃
Left pointers should be able to move till the point in which the max diff is less than k, so it should be inside a while loop👍, also n^2 solution was accepted?
Just like the monotonic stack , in which the elements are stacked ascending or descending , it is also monotonic queue in which elements are added increasing or decreasing but one at a time in a single queue
Have added Timestamps for anyone just here to solve the problem, just watch starting 14mins 🌚, for others watch depending on jitni himmaat bachi ho 💀
.
Sub nhi bharvaye naa aap logo ne 🥺
First like and first comment. I solved this question with the help of the deque method
Awesome man 😮💨🫡❤️
Last approach was great man!!!
The video is informative, first approach was very intuitive. Never thought of 2nd and 3 rd approach
THank you! I didn't hear previously about `multiset` data structure...
Awesome video!
Bro what an explanation you cover a lot of concepts with this question thankyou brother and make such videos daily to help us
How about using 2 segment trees ?
One to track maximum the other for minimum
Hi Aryan. It's quite fine explanation. I have one question; can we use ordered_map instead of multiset?
I tried it with two ordered maps and the max value i was storing with -nums[j] trick because the if nums[j] is maximum in window then-nums[j] will be least but unfortunately it failed , some runtime error was coming and the code was logically correct and also that condition if mp[nums[j]] == 0 so do mp.erase(nums[j]) but also the runtime error was coming and also when keys are negative, in the max ordered map , i was not able to access the key with this method mp[nums[j]] because -ve indices not exist so i used mp.at(nums[j]) but it was failing
so any 2 elements means all possible 2 elements .. not any of the 2 elements in subarray???
How can we be sure that the front will always contain the elements which are lesser than the minimum index,since we are popping from the front
Amazing
I have seen a lot of comments on people struggling with Min and Max heap including me,🥲
Like this comment you want Aryan bhaiya to make a video on the basics and in dept analysis of priority queue(MIN heap and MAX heap) , how to use them as it is a lot confusing.
🙌🙌🙌
Bhaiya kal ke Leetcode ka Q3 soln chahiye.. mera TLE aa rha tha...
RIght, Mera v TLE agya ta 3rd question me
intuition -
intialize a flag = 1 and if nums[i] is !flag (i.e. 0), increase the count and toggle the flag.
So the main idea is to toggle the flag whenever we encounter the alternate value.
We don't need to change the array manually, just need to create it hypothetically.
class Solution {
public int minOperations(int[] nums) {
int ans = 0;
int flag = 1;
for(int i = 0; i < nums.length; i++) {
if(nums[i] != flag) {
ans++;
flag = 1 - flag;
}
}
return ans;
}
}
@@ajringtones9574 thanks bhai
Just maintain the indexes of min element and max element in window, this will require constant space
this will not work bro , imagine your min element was at starting of your subarray and next min is at 2nd last of subarray , so you will have to iterate whole subarray to know which is next min or max element
@@codebreaker6578 class Solution {
public:
int longestSubarray(vector& nums, int limit) {
int min_el = nums[0];
int min_idx = 0;
int max_el = nums[0];
int max_idx = 0;
int n = nums.size();
int ans = 1; // minimum subarray size
int left = 0; // left pointer
int right = 0; // right pointer
while (right < n) {
// Update min and max elements and their indices
if (min_el >= nums[right]) {
min_idx = right;
min_el = nums[right];
}
if (max_el limit) {
// Move left pointer to right of min_idx or max_idx
left = min(min_idx, max_idx) + 1;
// Update min_el and max_el to the new values in the window
min_el = nums[left];
max_el = nums[left];
// Find new min_idx and max_idx in the new window
for (int i = left; i = nums[i]) {
min_idx = i;
min_el = nums[i];
}
if (max_el
When the condition violates, it is either due to min or max, so we need to update or new min or max at right index and then based on weather it is min or max, find second min or second max from the existing window.
Very informative video, Thanks bro, you explained it very clearly in all approaches. Loved your explaination. I was thinking of heaps before solving but was unable to achieve the logic while shrinking and deleting. Multiset was never in mind🙃
Cna anyone correct me please?
```
int longestSubarray(vector& nums, int limit) {
int r = 0, l = 0, ans = 1;
int maxi = nums[r], mini = nums[l];
while(r
Left pointers should be able to move till the point in which the max diff is less than k, so it should be inside a while loop👍, also n^2 solution was accepted?
@@Sai-fz2yn yes excepted
puri dekhi 1.5 hrs lagi , par pura samajh aya .
I didnt understand the min heap max heap approach , did it using the multiset by taking hint, atleast my thought process is developing
Just like the monotonic stack , in which the elements are stacked ascending or descending , it is also monotonic queue in which elements are added increasing or decreasing but one at a time in a single queue
Sorry, I think, you are a great engineer, but unfortunately, not a teacher.
Looks like you are none
@@rohitkumarpilania94 yeah just like you.
Yeah , he was not able to intuitively explain the last deque approach
But yeah that happens , but it not means he is not a great teacher, his other videos are mind blowing
I think he explained really well all the approaches personally
Thanks a lot bhaiya..!!! tbh,Min heap and max heap wala portion thoda confusing tha,so i did from two pointers only,but veryy well explained,tysm