LeetCode 1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit

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

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

  • @TheAlexanderEdwards
    @TheAlexanderEdwards 2 ปีที่แล้ว +1

    tremendously helpful, thank you!

  • @walidzein1
    @walidzein1 2 ปีที่แล้ว +1

    Thanks man!

  • @littlebox4328
    @littlebox4328 2 ปีที่แล้ว +1

    you must be using your sharingan to be this good!!! Thanks man!!

    • @KacyCodes
      @KacyCodes  2 ปีที่แล้ว +1

      Just trying to awaken my susanoo xD

  • @djslimcodes2337
    @djslimcodes2337 2 ปีที่แล้ว

    Hey @Kacy Codes , how was your Amazon interview?

  • @atanumondal8078
    @atanumondal8078 2 ปีที่แล้ว

    Hey Kacy,
    Plesae Please Please make LC 1950. Maximum-of-minimum-values-in-all-subarrays

    • @KacyCodes
      @KacyCodes  2 ปีที่แล้ว

      I solved it right now, but it's pretty tough for a medium. I don't have time to make a video so I'll link my submission and try to explain the algorithm.
      1. Initialize your result array with the minimum integer value.
      2. Use an increasing stack to find the indexes of the previous lesser and next lesser elements for each element in the input array. Calculate the distance from the previous lesser to the next lesser, and that's what length subarray the value at the current index you're processing is a minimum value of.
      3. Update the max value in your result array for that length. (Initially I tried updating all the lengths that are greater or equal to the length, but the algorithm becomes n^2 if you do that.)
      You'll repeat step 2 & 3 for every element in the input array.
      4. This last part was tricky for me but the intuition is that it's similar to a prefix sum. Say 100 is the max value for subarray length of 2 and you have that in your result array. But also in your result array for length 1 you only have 50. Well that doesn't make sense because if 100 is the max value for subarrays of length 2, it must also be the max value for subarrays of length 1. So to finish the "prefix sum" on the result array, I loop from the end to the beginning doing result[i] = Math.max( result[i], result[i + 1] );
      leetcode.com/submissions/detail/724992785/
      If you have trouble at any step I would print out your result array in your code and compare it to my solution in another tab or something.

    • @atanumondal8078
      @atanumondal8078 2 ปีที่แล้ว +1

      @@KacyCodes Great. I have no words to thank you😊. Will look into it.