Sliding Window Leetcode Problem Solved with JavaScript

Поделиться
HTML-код
  • Опубликовано: 11 сен 2024

Комментарии • 23

  • @surayamelina
    @surayamelina 10 месяцев назад +5

    First of all thank you for calling sliding window problems hard!! So nice to have someone validate that for a change, instead of calling them all easy

    • @JamesQQuick
      @JamesQQuick  10 месяцев назад +1

      Oh I think it’s super tricky especially early on. You’re not alone at all!

  • @kizhissery
    @kizhissery 11 месяцев назад +6

    using two pointer left and right .
    let n = nums.size();
    let ans = Number.MAX_SAFE_INTEGER;
    let l = 0,r = 0,sum = 0;
    while(r=target){
    if(sum>=target){
    ans = min(ans,r-l+1);
    sum -= nums[l++];
    }
    }
    r++;
    }
    if(ans== Number.MAX_SAFE_INTEGER) return 0;
    return ans;

  • @iHidden1
    @iHidden1 11 месяцев назад +3

    Thanks James, really good explanation and not rushed through. Would love for this to be a whole series for the 150 problems maybe? Thanks again! Good luck!

  • @bp-ob8ic
    @bp-ob8ic 11 месяцев назад +1

    Loved getting to see this live. Thank you, James, and keep them coming.

  • @eren-qu9wm
    @eren-qu9wm 2 месяца назад

    first i think sliding window algo is hard, then i realised it's very simple. Thank for the tutorial.

  • @karimbouche3563
    @karimbouche3563 8 месяцев назад

    Yeah bro personally I am interested of the solution of 0(n log(n)) and this kind of challenges,so please continue posting that kind of videos , but essentially it was a wonderful explanation congratulations

  • @tenthlegionstudios1343
    @tenthlegionstudios1343 11 месяцев назад

    For me, any time I can get the index's mixed up it becomes especially hard. Things like sliding window or zig zag 2d arrays throw me off. Thanks for the content!

  • @cannabisanomaly
    @cannabisanomaly 7 месяцев назад

    Also, in the leetcode description for the follow up, O(n) is better in time complexity than O(n log(n)), right? Maybe they meant to put O(log(n)) instead?

    • @LeetStack
      @LeetStack 29 дней назад +1

      no O(n) scales multiplicatively and O(n log(n)) scales logarithmically(think backwards shrinking exponents).

    • @cannabisanomaly
      @cannabisanomaly 9 дней назад +1

      @@LeetStack You're 100% correct. I always get confused on logarithmic vs quasilinear complexities, in terms of which is the faster one 😅

  • @girishpandey9976
    @girishpandey9976 7 месяцев назад

    Thanks James it really good.....

  • @sandhyasrini_
    @sandhyasrini_ 11 месяцев назад

    Great explanation! Thank you for the video!

    • @JamesQQuick
      @JamesQQuick  11 месяцев назад

      So glad you enjoyed it! Have you done sliding window problems before?

  • @Hhammer
    @Hhammer 11 месяцев назад

    Thanks James this is fun

    • @JamesQQuick
      @JamesQQuick  11 месяцев назад

      So glad you enjoyed it. I have fun with these

  • @cannabisanomaly
    @cannabisanomaly 7 месяцев назад

    0:41 that company is going through some budget cuts and fresh dry erase markers just aren't in the picture

  • @antonbarbakov9848
    @antonbarbakov9848 10 месяцев назад +1

    Nothing clear , but veeery interesting))

    • @JamesQQuick
      @JamesQQuick  10 месяцев назад +1

      Anything I could help explain better?

  • @vamshireddy8187
    @vamshireddy8187 6 месяцев назад

    nice

  • @r.avinashkumar5372
    @r.avinashkumar5372 4 месяца назад

    @JamesQQuick please check this solution.
    No nested for loops , no nested if condition . Please pin , if correct. I am new to DSA.
    let nums = [2, 3, 1, 2, 4, 3];
    let target = 7;
    var minSubArrayLen = function (target, nums) {
    let finalLength = nums.length;
    let initialIndex = 0;
    let previousTotal = nums[0];
    let i = 0;
    let sumQueue = [nums[0]];
    while (i < nums.length ) {
    if (previousTotal >= target) {
    finalLength = Math.min(finalLength, i + 1 - initialIndex);
    let val = sumQueue.shift();
    previousTotal -= val;
    initialIndex = initialIndex + 1;
    } else {
    i = i + 1;
    sumQueue.push(nums[i]);
    previousTotal += nums[i];
    }
    }
    return finalLength;
    };
    console.log(minSubArrayLen(target, nums));

    • @LeetStack
      @LeetStack Месяц назад

      your solution doesn't account for if the array has 0 subarrays