1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit | 3 Approaches

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ส.ค. 2024

ความคิดเห็น • 39

  • @ARYANMITTAL
    @ARYANMITTAL  2 หลายเดือนก่อน +10

    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 🥺

  • @ankurverma4042
    @ankurverma4042 2 หลายเดือนก่อน +8

    First like and first comment. I solved this question with the help of the deque method

    • @ARYANMITTAL
      @ARYANMITTAL  2 หลายเดือนก่อน +2

      Awesome man 😮‍💨🫡❤️

  • @prajitbanerjee8226
    @prajitbanerjee8226 2 หลายเดือนก่อน +2

    Last approach was great man!!!

  • @krishpoptani7862
    @krishpoptani7862 2 หลายเดือนก่อน

    The video is informative, first approach was very intuitive. Never thought of 2nd and 3 rd approach

  • @IK-xk7ex
    @IK-xk7ex 2 หลายเดือนก่อน +4

    THank you! I didn't hear previously about `multiset` data structure...

  • @user-xc6ez1kj6q
    @user-xc6ez1kj6q 2 หลายเดือนก่อน +1

    Awesome video!

  • @varunjain3947
    @varunjain3947 2 หลายเดือนก่อน

    Bro what an explanation you cover a lot of concepts with this question thankyou brother and make such videos daily to help us

  • @rahulz337
    @rahulz337 25 วันที่ผ่านมา

    How about using 2 segment trees ?
    One to track maximum the other for minimum

  • @rushirajparekh9079
    @rushirajparekh9079 2 หลายเดือนก่อน

    Hi Aryan. It's quite fine explanation. I have one question; can we use ordered_map instead of multiset?

    • @SidharthjainSingla
      @SidharthjainSingla 2 หลายเดือนก่อน

      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

  • @MaheshKumar-mh9mj
    @MaheshKumar-mh9mj 2 หลายเดือนก่อน +1

    so any 2 elements means all possible 2 elements .. not any of the 2 elements in subarray???

  • @arjunc1482
    @arjunc1482 2 หลายเดือนก่อน

    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

  • @shashankarora2945
    @shashankarora2945 2 หลายเดือนก่อน

    Amazing

  • @xenondeltor7925
    @xenondeltor7925 2 หลายเดือนก่อน +3

    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.

  • @RohitKumar-hn6wj
    @RohitKumar-hn6wj 2 หลายเดือนก่อน

    🙌🙌🙌

  • @ITACH1688
    @ITACH1688 2 หลายเดือนก่อน +1

    Bhaiya kal ke Leetcode ka Q3 soln chahiye.. mera TLE aa rha tha...

    • @mohit7717
      @mohit7717 2 หลายเดือนก่อน

      RIght, Mera v TLE agya ta 3rd question me

    • @ajringtones9574
      @ajringtones9574 2 หลายเดือนก่อน +4

      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;
      }
      }

    • @ITACH1688
      @ITACH1688 2 หลายเดือนก่อน

      @@ajringtones9574 thanks bhai

  • @atulrulers
    @atulrulers 2 หลายเดือนก่อน

    Just maintain the indexes of min element and max element in window, this will require constant space

    • @codebreaker6578
      @codebreaker6578 2 หลายเดือนก่อน +1

      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

    • @atulrulers
      @atulrulers 2 หลายเดือนก่อน

      @@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

    • @atulrulers
      @atulrulers 2 หลายเดือนก่อน

      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.

  • @kergeysarjakin5592
    @kergeysarjakin5592 2 หลายเดือนก่อน

    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🙃

  • @mohit7717
    @mohit7717 2 หลายเดือนก่อน

    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

    • @Sai-fz2yn
      @Sai-fz2yn 2 หลายเดือนก่อน

      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?

    • @DevJunk-mf1dc
      @DevJunk-mf1dc 2 หลายเดือนก่อน +1

      ​@@Sai-fz2yn yes excepted

  • @ajayprabhu465
    @ajayprabhu465 2 หลายเดือนก่อน

    puri dekhi 1.5 hrs lagi , par pura samajh aya .

  • @harshdiwase1941
    @harshdiwase1941 2 หลายเดือนก่อน

    I didnt understand the min heap max heap approach , did it using the multiset by taking hint, atleast my thought process is developing

    • @SidharthjainSingla
      @SidharthjainSingla 2 หลายเดือนก่อน

      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

  • @karankamath7031
    @karankamath7031 2 หลายเดือนก่อน +3

    Sorry, I think, you are a great engineer, but unfortunately, not a teacher.

    • @rohitkumarpilania94
      @rohitkumarpilania94 2 หลายเดือนก่อน +2

      Looks like you are none

    • @karankamath7031
      @karankamath7031 2 หลายเดือนก่อน

      @@rohitkumarpilania94 yeah just like you.

    • @SidharthjainSingla
      @SidharthjainSingla 2 หลายเดือนก่อน

      Yeah , he was not able to intuitively explain the last deque approach

    • @SidharthjainSingla
      @SidharthjainSingla 2 หลายเดือนก่อน

      But yeah that happens , but it not means he is not a great teacher, his other videos are mind blowing

    • @Dota2Vibes
      @Dota2Vibes 2 หลายเดือนก่อน

      I think he explained really well all the approaches personally

  • @hardiksinghbhadoria6782
    @hardiksinghbhadoria6782 2 หลายเดือนก่อน +2

    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