Sliding window technique - Inside code

Поділитися
Вставка
  • Опубліковано 21 січ 2025

КОМЕНТАРІ • 97

  • @mubasakz7651
    @mubasakz7651 11 місяців тому +51

    Great explanation! Someone get this man an award!

    • @recursion.
      @recursion. 3 місяці тому

      alright I just gave him a turing award now piss off

  • @biggn7441
    @biggn7441 2 роки тому +31

    Your videos are impressive! Can you do all of the major technical interview patterns and the intuition behind them? Thanks 💯

  • @sameerhasan7376
    @sameerhasan7376 Рік тому +7

    Beautiful explanation! Thank you! Was struggling with this for quite some time.

  • @asunthakeded4967
    @asunthakeded4967 3 місяці тому +2

    sliding window made very very easier. Now I will be able to solve 80% of the problem related to sliding easily without thinking much and by visualizing

  • @probabilitycodingisfunis1
    @probabilitycodingisfunis1 2 роки тому +11

    Amazing videos, content is explained so well with impressive animations!

  • @algorithmdatastructures9244
    @algorithmdatastructures9244 Рік тому +2

    Best video on this topic all over youtube. 😍

  • @abirbanerjee839
    @abirbanerjee839 Місяць тому

    This is absolutely brilliant ❤. Please keep posting such gems 🙏🏼.
    Love from India 🇮🇳

  • @sarthaksharma9322
    @sarthaksharma9322 6 місяців тому +1

    Beautiful visualisation! and explanation of time complexities and algorithm, love the video so much!!❤

  • @megamehdi89
    @megamehdi89 2 роки тому +8

    Please continue making videos. I love them ❤️

  • @shaheerzaman620
    @shaheerzaman620 2 роки тому +12

    Nice video! Here is the solution for vowels
    # max number of vowels in a string of size k
    def max_vowels(s, k):
    vowels = ("a", "e", "i", "o", "u")
    maxvowels = 0
    for ch in s[:k]:
    if ch in vowels:
    maxvowels += 1
    total = maxvowels
    for i in range(len(s) - k):
    if s[i] in vowels:
    total -= 1
    if s[i + k] in vowels:
    total += 1
    maxvowels = max(maxvowels, total)
    return maxvowels

    • @insidecode
      @insidecode  2 роки тому +3

      perfect! good job

    • @DAOXINNo15
      @DAOXINNo15 4 місяці тому

      Would you say in this optimal way to be O(n) then because of how the two if statements are linear and all right?

  • @ESLRepeat
    @ESLRepeat 6 місяців тому +1

    thanks man, you explained the thing really well. I had fun taking the class

  • @chaos.n.dissonance
    @chaos.n.dissonance 7 місяців тому

    Thank you. I've been struggling to wrap my mind around ChatGPT's explanation of sliding window technique for a few days now, this is so much simpler.
    def count_vowels_efficientcode(word:str, k:int) -> int:
    VOWELS = set('aeiou')
    max_vowels:int = 0
    n:int = len(word)
    if n

  • @iezioaudi22
    @iezioaudi22 10 місяців тому +1

    this was the best explanation I have seen!
    THANK YOUU!

  • @ehapahmed1069
    @ehapahmed1069 2 місяці тому

    your explanation is just simply brilliant ... I love it

  • @felixolesen809
    @felixolesen809 Місяць тому

    These diagrams are so helpful! Thanks for the great video and explanation!

  • @rnniloyiii9082
    @rnniloyiii9082 5 місяців тому +39

    The name should be caterpillar 🙂

  • @shekharpatil4947
    @shekharpatil4947 25 днів тому

    I never understood this kind of usefull concept within this short time of period.

  • @Samad_27
    @Samad_27 2 роки тому +8

    Plz make more videos no one can beat your level Absolutely Brilliant

  • @preminfi4887
    @preminfi4887 5 місяців тому

    Great explanation! Awesome visualisation, it is easy to understand the problem and the solution.

  • @DailyCuteThingsInside
    @DailyCuteThingsInside 2 роки тому +1

    Absolute genius, please continue making videoss

  • @badermuteb4552
    @badermuteb4552 2 роки тому +4

    nicely done. the best tutorial ever. please continue making new vide like this,
    here is my solution: is it correct?
    def mnv(s,k):
    v = "aeoiu"
    ms =0
    for i in range(k):
    if s[i] in v:
    ms +=1
    max_substring = ms
    for i in range(len(s) - k):
    if s[i] in v:
    ms -= 1
    if s[i+k] in v:
    ms += 1
    max_substring = max(max_substring, ms)
    return max_substring
    how would you solve it?????

  • @codelinx
    @codelinx Рік тому +1

    Great channel. Great illustrations and examples....🙋‍♂️

  • @sainithyamsani4062
    @sainithyamsani4062 2 роки тому +1

    keep uploading this kind of videos. your videos are awesome!!!

  • @tankstechtutorials5721
    @tankstechtutorials5721 10 місяців тому

    great video. Had fun brushing up on this algorithm.
    function maxNumOfVowels(s, k) {
    let maxNum = 0;
    function isVowel(c) {
    return /[aeiou]/.test(c) ? 1 : 0;
    }
    let leftBound = 0;
    let rightBound = k-1;
    let firstSlice = s.substring(leftBound, k);
    for (let index = 0; index < firstSlice.length; index++) {
    const element = firstSlice[index];
    maxNum += isVowel(element);
    }
    let currentCount = maxNum;
    while (rightBound < s.length - 1) {
    leftBound++;
    rightBound++;
    if(isVowel(s[leftBound-1])) {
    currentCount -= 1;
    }
    if (isVowel(s[rightBound])) {
    currentCount += 1;
    }
    maxNum = Math.max(currentCount, maxNum);
    }
    return maxNum;
    }

  • @pierrehebert9743
    @pierrehebert9743 2 роки тому +2

    When you first introduced the problem, we wanted to pick out the best set of five books, not what the price of the best set was. That means we were looking for argmax rather than max. so the code would be closer to:
    def best_set_for_price(items, k):
    if len(items) max):
    max = offset
    argmax = i + 1
    return argmax
    As a bonus, note how we never need to calculate the initial sum.

  • @ysmerde2933
    @ysmerde2933 Рік тому +1

    You know you are gonna learn something when the English speaker has an foreign accent ty for the tuto dude you insane

  • @anniamatthews6803
    @anniamatthews6803 3 місяці тому

    great explanation! you made this very simple

  • @rishabhranjan5162
    @rishabhranjan5162 7 місяців тому

    I love these kind of explanations

  • @momal51
    @momal51 4 місяці тому

    Very nice explanation, thanks man !

  • @methylie
    @methylie 2 роки тому +1

    Your lectures are good 😊.keep posting vedios

  • @TechDoctorMalayalam
    @TechDoctorMalayalam 10 місяців тому

    Great explanation and illustration

  • @biloliddinfarkhodov3956
    @biloliddinfarkhodov3956 Рік тому

    This is the best explanation I have ever watched. Thank you!

  • @ramseykarr6870
    @ramseykarr6870 4 місяці тому

    Best explanation ever!

  • @bracuiansarefabulous6572
    @bracuiansarefabulous6572 28 днів тому

    def max_vowel(string, k):
    vowel = 'aeiou'
    total = len([i for i in string[0:k] if i in vowel])
    mx = total
    for i in range(len(string)-k):
    if string[i] in vowel:
    total -= 1
    if string[i+k] in vowel:
    total += 1
    mx = max(total, mx)
    return mx

  • @probabilitycodingisfunis1
    @probabilitycodingisfunis1 2 роки тому +1

    How do you make these animations?

  • @kaushik.aryan04
    @kaushik.aryan04 2 роки тому +1

    please make more videos like this on recursion medium hard questions

  • @odell4709
    @odell4709 Рік тому

    Made things very clear, thank you so much

  • @shreyaskulkarni4805
    @shreyaskulkarni4805 Рік тому

    Impressive Explanation ❤️

  • @paulsingh11
    @paulsingh11 10 місяців тому

    Sounds like the key word to use the Sliding Window is "contiguous" when dealing with an Array?

  • @R_SinghRajput
    @R_SinghRajput 7 місяців тому

    Crazy explanation 🔥😎

  • @bobbrysonn
    @bobbrysonn 5 місяців тому

    Great explanation!

  • @peacepeace9462
    @peacepeace9462 10 місяців тому

    i dont understand why the brute force for best_total_price has the "for i in range(len(prices)-k+1)" instead of "for i in range(len(prices)-k)". Could anyone explain this?

    • @tapin130505
      @tapin130505 10 місяців тому

      because the last element should be included

    • @peacepeace9462
      @peacepeace9462 10 місяців тому

      I figured out why. len(prices)-k+1 is the starting index of the last combination of the books. The calculations of brute force and sliding window have different index ranges.

  • @oddfeeling7956
    @oddfeeling7956 25 днів тому

    SHould've put the answer at then end of the video. Paused the video and played when done thinking the answer would be revealed to validate. Nice video though. Thanks

  • @cecitorresmx
    @cecitorresmx 9 місяців тому

    Your video help me a lot! Thank you

  • @aahringer
    @aahringer Рік тому

    Well done, thank you for sharing!

  • @internetandcomputerprobe4426
    @internetandcomputerprobe4426 3 місяці тому

    Really Informative

  • @ananyieie
    @ananyieie 9 місяців тому

    thankyou sir, great explanation☺

  • @rodrigoelias1987
    @rodrigoelias1987 8 місяців тому

    Nice content, dropping a like right now

  • @Millicx
    @Millicx 2 роки тому

    Priceless video

  • @rodrigoelias1987
    @rodrigoelias1987 8 місяців тому

    here is a node solution for the exercise at the end of the video:
    import { strictEqual } from "assert";
    const vowels = new Map([
    ["a", "a"],
    ["e", "e"],
    ["i", "i"],
    ["o", "o"],
    ["u", "u"],
    ]);
    const isVowel = (v: string) => vowels.has(v);
    const countVowels = (string: string, span = 5) => {
    let vowelsCount = 0;
    let maxVowels = 0;
    for (let i = 0; i < string.length; i++) {
    if (i - span - 1 > -1) {
    if (isVowel(string[i - span - 1])) {
    vowelsCount--;
    }
    }
    if (isVowel(string[i])) {
    vowelsCount++;
    }
    if (vowelsCount > maxVowels) {
    maxVowels = vowelsCount;
    }
    }
    return maxVowels;
    };
    const input = "bacacbefaobeacfe";
    strictEqual(
    countVowels(input),
    4,
    "The expected ammount of vowels was 4, got " + countVowels(input)
    );

  • @callmeitsh
    @callmeitsh 8 місяців тому

    Nice man 👍
    Thank u bro ♥️

  • @the-ultimate-og
    @the-ultimate-og 8 місяців тому

    awesome video!

  • @noname_2108
    @noname_2108 Рік тому

    very helpful, thanks!

  • @Syuzaki1301
    @Syuzaki1301 10 місяців тому

    tried this in JS. Used some helper functions so I don't dilute the main function:
    // HELPER FUNCTIONS
    function isVowel(str) {
    let vowels = "aeiou";
    return vowels.includes(str);
    }
    function countVowels(str) {
    let count = 0;
    for (let i = 0; i < str.length; i++) {
    if (isVowel(str[i])) count += 1;
    }
    return count;
    }
    // MAIN FUNCTION
    function maxVowels(s, k) {
    let total = countVowels(s.slice(0, 5));
    let maxVowels = total;
    for (let i = 0; i < s.length - k; i++) {
    if (isVowel(s[i])) total -= 1;
    if (isVowel(s[i + k])) total += 1;
    if (maxVowels < total) maxVowels = total;
    }
    return maxVowels;
    }

  • @GurtDovletov
    @GurtDovletov 11 місяців тому

    Very good explanation woow

  • @____r72
    @____r72 2 роки тому

    this channel is so top

  • @averrows
    @averrows 2 роки тому

    great video! what software did you use to create this video?

    • @insidecode
      @insidecode  2 роки тому +2

      Thanks! I used PowerPoint

  • @hammadhassan9014
    @hammadhassan9014 8 місяців тому

    you are genius

  • @Fabreg01
    @Fabreg01 2 роки тому +1

    Great video!!. This technique can also be leveraged in rolling hash calculations.
    Solution to the sliding window to calculate max vowels count in a string
    class Solution:
    def maxVowels(self, s: str, k: int) -> int:
    if len(s) < k:
    return 0
    total = 0
    lookUpv = {'a':True, 'e':True, 'i':True, 'o':True, 'u':True}
    for c in s[:k]:
    if c in lookUpv:
    total += 1
    maxTotal = total
    for i in range(len(s)-k):
    char_to_add = s[i+k]
    if char_to_add in lookUpv:
    total += 1
    char_to_remove = s[i]
    if char_to_remove in lookUpv:
    total -= 1
    maxTotal = max(maxTotal,total)
    return maxTotal

    • @insidecode
      @insidecode  2 роки тому +1

      Yes, I'm planning a video on Rabin-Karp algorithm, which uses rolling hash

    • @insidecode
      @insidecode  2 роки тому +1

      Hey, the video on Rabin-Karp is out

    • @Fabreg01
      @Fabreg01 2 роки тому

      @@insidecode Great!!.Thanks

  • @charann18
    @charann18 2 роки тому

    Just awesome!!!!

  • @DiogoSilva-xx8nz
    @DiogoSilva-xx8nz 9 місяців тому

    awesome channel

  • @whiz-code
    @whiz-code 6 місяців тому

    I have come up with a solution but it seems like brute force, and if so, someone help me optimize it:
    static int maxVowelsNum(String s, int k) {
    var list = List.of('a', 'e', 'i', 'o', 'u');
    var p1 = 0;
    var p2 = k + 1;
    var max = 0;
    var count = 0;
    while (p2 < s.length()) {
    var chars = s.substring(p1, p2);
    for (var ch : chars.toCharArray()) {
    if (list.contains(ch)) {
    count++;
    }
    }
    max = Math.max(max, count);
    p1++;
    p2++;
    count = 0;
    }
    return max;
    }
    Thank you.

  • @kurdi_x5842
    @kurdi_x5842 2 роки тому

    thaankyou it is so clear

  • @chanukachathurangawk
    @chanukachathurangawk 6 місяців тому

    Thanks a lot.

  • @prathagautam9872
    @prathagautam9872 9 місяців тому

    To the point!

  • @subee128
    @subee128 8 місяців тому

    thanks

  • @SanjuKumar-hk8yy
    @SanjuKumar-hk8yy 2 роки тому +1

    I like your videos because your content with animation and your hard work, love you dude❤️. But I am not gay.

  • @devmahad
    @devmahad 3 місяці тому

    thanks :)

  • @JohnDoe-hl2qm
    @JohnDoe-hl2qm 24 дні тому

    legend

  • @ericlongteaches
    @ericlongteaches 5 місяців тому

    5:03 I have no clue what you just said here

  • @benhassineyoussef738
    @benhassineyoussef738 5 місяців тому

    "the size doesnt have an impact"

  • @ShivamKadam-bc4dn
    @ShivamKadam-bc4dn 3 місяці тому

    2.07

  • @nullptr.
    @nullptr. 6 місяців тому

    Thanks