Sliding Window Technique - Algorithmic Mental Models

  Рет қаралды 351,771

Ryan Schachte

Ryan Schachte

Күн бұрын

Пікірлер: 404
@omkarpat
@omkarpat 5 жыл бұрын
One of the best explanations of this concept. Please make some more "Algorithmic Mental Models" based videos.
@CEOofTheHood
@CEOofTheHood 3 жыл бұрын
dude u seriously need to make more of these. Ill pay for them.
@andrewblaines
@andrewblaines 3 жыл бұрын
Agreed! These are great. More "Algorithmic Mental Models" for dynamic programming, backtracking, etc. would be extremely helpful. Thanks for the video!
@veliea5160
@veliea5160 3 жыл бұрын
it is not one of the best, it is the best
@Mogwai88
@Mogwai88 3 жыл бұрын
@@CEOofTheHood Yes. Please make more of these. Especially with your new freecodecamp traffic this would really take off.
@AhmadRafique-yn8ry
@AhmadRafique-yn8ry Ай бұрын
Agreed
@jacktrainer4387
@jacktrainer4387 4 жыл бұрын
I've never before seen a CS video that approached CS like math (i.e., here are the concepts, here are some keywords to look out for so you know when to apply these concepts, here are a few examples). The world needs 1,000 more videos like this (DP, Linked Lists, Trees, Graphs, etc). Fantastic work!
@maxdrojjin6984
@maxdrojjin6984 5 жыл бұрын
This is really the best video (or any, really) explanation of the topic I have found. Really hope this would become more popular. Thank you!
@divyanirao4279
@divyanirao4279 4 жыл бұрын
This video is amazing. hope you make more videos on "mental models" such as for dynamic programming.
@michadobrzanski2194
@michadobrzanski2194 3 жыл бұрын
Max subarray sum of k is also a dynamic programming problem. You reuse the previous max result and update it. So it is like dynamic programming with space complexity O(1).
@fahadhayat_
@fahadhayat_ 3 жыл бұрын
Dude please create a playlist for the other techniques as well!!! This is gold!
@zappist751
@zappist751 Жыл бұрын
Does he have a playlist for this?
@maggiesayabie
@maggiesayabie 4 жыл бұрын
Who even has the audacity to dislike such a video?? The best and simplest explanation of sliding window concept i have ever come across. Thank you sir!
@priyamrai5915
@priyamrai5915 8 ай бұрын
will it work if array has negative elements. I think no.
@sujityadawad
@sujityadawad 5 жыл бұрын
Was struggling a lot with sliding window problems earlier, your explanation really simplified those problems. I really appreciate your effort. Thank you! Please post more videos.
@4DChess
@4DChess Жыл бұрын
Ryan where'd you go 😭😭 nobody else made vids as good as yours. I need you to cover the other topics too 🥺🥺🥺🥺
@yynnooot
@yynnooot 2 жыл бұрын
Do you have a series for these Algorithmic Mental Models? Your video was extremely helpful and I would love to see more of these!
@saugatkarki3169
@saugatkarki3169 5 ай бұрын
I looked into it and looks like this "Algorithmic mental models" video is the most viewed video on your channel. Upon reading the comments, people seems to really love it and are asking more of it covering other topics. Genuinely curious, why didn't you make more of this type of vide, that clearly seems to be the most viewed one on your channel?
@shriduttkothari
@shriduttkothari 2 жыл бұрын
This is the only video which made me understand dynamic window sizing algorithm... Thank you so much 🥰
@zackgrey4472
@zackgrey4472 3 жыл бұрын
For the smallest subarray code, wouldn't the time complexity be O(n^2) still because you added a while loop inside of your for loop? I recall the purpose of the sliding window technique is to convert quadratic time to linear, is this only for the fixed length and doesn't apply to dynamic? Or am I just misunderstanding the time complexity? Thanks for the wonderful explanation!!
@potatocoder5090
@potatocoder5090 2 жыл бұрын
I have struggled with this concept for a really long time, but not anymore :) Your explanations were absolutely beautiful and I'm excited to solve some sliding window problems now! Can you please create more Algorithmic Mental Model videos? We'd all be super grateful!
@diazdiaz3084
@diazdiaz3084 4 жыл бұрын
Subscribed 😠😠 .. make more on algorithms ..
@dr_920
@dr_920 4 жыл бұрын
One of the best tutorials I have even seen. Thanks.
@svdfxd
@svdfxd 5 жыл бұрын
One of the The best videos to explain Sliding windows concept. Request you to make other such videos that will help in tech interviews.
@uzvalmallepeddi8962
@uzvalmallepeddi8962 3 жыл бұрын
I wouldn’t resist to pay a 1000$ to watch such videos. Brilliant piece of work! Hope you do more videos on “Mental models”
@silvahawk
@silvahawk 3 жыл бұрын
I have always been calculating the first windowSum in a separate loop before starting to "move" the window in a separate loop. I never knew you could do both within the same loop without using a nested loop. This is amazing!
@neerajkulkarni6506
@neerajkulkarni6506 3 жыл бұрын
This is fantastic! Please make more of these 'mental model' videos. There too many videos out there that jump straight to the solution without any discussion of how to approach and generalize a problem. We need more video's like this!
@pjmiravalle
@pjmiravalle 2 жыл бұрын
I came across this video after struggling with sliding window problems in preparation for my upcoming Google interview, and just wanted to thank you for the super clear explanation. I'm feeling much more confident with these problems now. 👍
@tomrobinson8290
@tomrobinson8290 2 жыл бұрын
Did you get into google? How was the interview!?
@giampierovanzzini1430
@giampierovanzzini1430 3 күн бұрын
This is a really great informative video, thanks for the hard work! I really like the breakdown to creating a mental model
9 ай бұрын
Thanks, I flew through LeetCode Sliding Window challenges after watching this.
@sedgeralt
@sedgeralt Жыл бұрын
Hands down the best explanation of the Sliding Window technique. Please do more algorithmic mental model videos!
@DonMamaril
@DonMamaril 5 жыл бұрын
I loved, looooved the format of this! Please continue to do these videos. Extremely helpful!!
@parthabhowmik4747
@parthabhowmik4747 2 жыл бұрын
The best lecture on sliding window technique
@rohitsuresh3292
@rohitsuresh3292 5 жыл бұрын
LeetCode Links of similar problems to practice: 1. leetcode.com/problems/sliding-window-maximum/ 2. leetcode.com/problems/shortest-subarray-with-sum-at-least-k/ 3. leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/
@VictorThomasCSC
@VictorThomasCSC 4 жыл бұрын
Holy shit great vid
@amreshgiri
@amreshgiri 2 ай бұрын
🎯 Key points for quick navigation: 00:09 *🧠 Sliding Window Technique Overview* - Overview of the sliding window technique as an algorithmic mental model, - Benefits of using sliding window over naive approaches, - Introduction to fixed size and dynamically resizing sliding window techniques. 02:00 *🪟 Fixed Size Sliding Window Technique* - Explanation of fixed size sliding window using a rectangular metaphor, - Example of maximizing sum of a contiguous subarray using fixed size sliding window, - Comparison with brute-force approach highlighting efficiency gains. 07:00 *📊 Identifying Sliding Window Problems* - Characteristics of problems suited for sliding window technique, - Common keywords and problem types (min/max, longest/shortest, containment), - Applicability across arrays, strings, and linked lists. 13:38 *🎯 Types of Sliding Window Problems* - Fixed length variant: Example with fixed size subarrays and maximizing sums, - Dynamic resizing variant: Handling variable window sizes dynamically, - Dynamic variant with auxiliary data structures: Examples involving hash maps for advanced constraints like distinct characters. 17:05 *🔄 Commonalities in Sliding Window Problems* - Sequential nature of problems and contiguous data groupings, - Criteria for solving problems (maximization, minimization, containment), - Unifying approach to algorithmic problem solving using sliding window technique. 22:16 *📊 Sliding Window Technique for Fixed Window Size* - Explanation of the fixed-size sliding window technique, - Detailed walkthrough of how to maintain and update the sliding window, - Example problem solved using the fixed-size sliding window approach. 24:58 *🎯 Dynamic Sliding Window for Variable Window Size* - Introduction to dynamic sliding window where window size can vary, - Problem-solving approach to find the smallest subarray with a given sum, - Demonstration of adjusting window size dynamically based on problem constraints. 33:03 *🧩 Sliding Window with Auxiliary Data Structure* - Application of sliding window technique with an auxiliary data structure, - Problem-solving for finding the longest substring with K distinct characters, - Strategy for using hash maps to optimize tracking of character frequencies in the sliding window. Made with HARPA AI
@subba18
@subba18 4 күн бұрын
how do we get the actual subarray itself instead of length n these examples.
@waseemmn2007
@waseemmn2007 6 ай бұрын
OMG, Best problem solving explanation I've ever seen. it is a really helpful, simple, and to the point, Thank you for this extraordinary video. Liked and subscribed 🙌
@shrey4489
@shrey4489 2 жыл бұрын
Great explanation! I'm a visual learner and I can see myself imagining these sliding window animations when I face array problems like this in the future.
@vicente3j
@vicente3j Жыл бұрын
Only 219k views... no way. This is by far some of the best CS-related content on KZbin, hands down. Amazing!!
@frozen_tortus
@frozen_tortus 4 жыл бұрын
I'm absolutelly blown away how this is explained.
@yashj1072
@yashj1072 3 ай бұрын
are you fucking kidding me? 7:16, I see that image and could immediately code it based on your previous graphical explanation.
@Aestheticslay_yay
@Aestheticslay_yay 2 жыл бұрын
//Java version of Longest substring with length k distinct characters public class DistinctWithKLetters { public static void main(String a[]){ Character[] charArray = {'A','A','A','H','H','B','B','B','B','B','I','I','I','C'}; System.out.println(findMaxLength(charArray, 2)); } private static int findMaxLength(Character[] charArray, int k) { Map map = new HashMap(); int start = 0; int maxValue = Integer.MIN_VALUE; int currentValue = 0; for(int i = 0; i < charArray.length ; i++) { map.put(charArray[i], map.getOrDefault(charArray[i], 0)+1); while(map.size() > k) { map.put(charArray[start], map.get(charArray[start])-1); if(map.get(charArray[start]) == 0){ map.remove(charArray[start]); } start++; currentValue = map.get(charArray[start]); //getting count of next character after deleted the first character. } maxValue = Math.max(++currentValue, maxValue); } return maxValue; }
@mumarkhan1898
@mumarkhan1898 2 жыл бұрын
In the second example 32:17 you're missing a condition in the inner while loop i.e windowStart should always be smaller or equal to windowEnd def get_smallest_sub_array_with_sum_greater_equal_to_a_value(a, k): left, right = 0, 0 min_window_size = float('+inf') current_sum = 0 while right < len(a): current_sum += a[right] while current_sum >= k and left
@zhenliu4779
@zhenliu4779 2 жыл бұрын
really good. thank you But I think at 31:32, we should do if(currentWindowSum == targetSum){ minWindowSize = Math.min(minWindowSize, windowEnd-windowStart+1)}
@go_lang_thang
@go_lang_thang Жыл бұрын
Hi Sir, I realyy respect you guy. Could you give me a list of Algorithmic Mental Models ? I wanna master DSA as soon as possible, bro !!!
@drkenny7928
@drkenny7928 2 жыл бұрын
for python lovers on the first question cur = 0 _max = cur t = [4,2,1,7] for i in range(len(t)): cur = sum(t[i:i+3]) if cur > _max: _max = cur else: cur = cur print(_max)
@TheDannyDeez
@TheDannyDeez 2 жыл бұрын
This video is pure gold, any chance you could make one on 2 pointers?
@MsHofmannsJut
@MsHofmannsJut Жыл бұрын
There's a high-pitched tone at the beginning of your video. It's very, very unpleasant, I reflexively closed the tab. Please remove it.
@vivienpmc
@vivienpmc Жыл бұрын
Great explanation :) anybody here knows if there are there any tutorials with Javascript examples?. TIA!
@sagaragrawal893
@sagaragrawal893 5 жыл бұрын
Very nice video on sliding window concept. Can you make similar video for Dynamic programming and cover some of the examples.
@shuvbhowmickbestin
@shuvbhowmickbestin 2 жыл бұрын
what a comprehensive video! Learnt a lot, liked and subbed. Keep bringing new content, you teach really good!
@fisher00769
@fisher00769 Жыл бұрын
I'm working on a Nonogram solver right now, but ran into some nasty time complexity issues with it, hopefully I can use this newfound knowledge to optimize it, really seems to be useful to that kind of problem.
@rashidibrahim979
@rashidibrahim979 3 жыл бұрын
for the last example, longest substring ... with k distinct. When hashmap.size > 2 is violated, why don't you just remove the starting characters all at once. For example, such as remove 'A' all at once. Instead of removing 'A' one by one. What is the point of decrementing the hashmap one by one ?
@samarbajaj
@samarbajaj 4 ай бұрын
What the ... the way you explained it my 10. your old can understand it. Wowowo
@TaraChand-eg6uk
@TaraChand-eg6uk 2 жыл бұрын
@Ryan : In case of smallestSubarray method, if targetSum is 9, it is failing, need to add another condition in it. private static int smallestSubarray(int targetSum, int[] input) { int currentWindowSum = 0; int minWindowSize = Integer.MAX_VALUE; int windowStart = 0; for (int windowEnd = 0; windowEnd < input.length; windowEnd++) { currentWindowSum += input[windowEnd]; while (currentWindowSum >= targetSum && windowEnd > windowStart ) { minWindowSize = Math.min(minWindowSize, windowEnd - windowStart + 1); currentWindowSum -= input[windowStart]; windowStart++; } } return minWindowSize; }
@TheMikediaz100
@TheMikediaz100 Жыл бұрын
For the smallest subarray solution in 32:51, can you explain if the algorithm work for this test case? arr = [1,1,1,1,1,1,1,1] k =11
@andresserron8596
@andresserron8596 3 жыл бұрын
The guy did the job... I personally process this better, as I enjoy spatial representations. It seems that includes the Kadane's algorithm they struggle to explain, always falling back to the logic demonstration. If you cannot touch it, it doesn't exist. This I can touch.
@alonbrim
@alonbrim 2 жыл бұрын
This video is pure gold!!! Great explanation . Everything is very clear. Thank you very much!
@dayshiasweet
@dayshiasweet Ай бұрын
This was a good video, but the continuous lip smacking made it hard to listen to.
@akashbharatwaj6945
@akashbharatwaj6945 4 жыл бұрын
Can you bring up a solution by having both negative and non negative numbers in the array??? For Example :find the Longest sub array with sum X for the given array {-5 8 -14 2 4 12}
@SocajowaRS
@SocajowaRS 4 жыл бұрын
Please more videos like this, they are so good. Maybe some graph related videos?
@plashless3406
@plashless3406 2 жыл бұрын
This is amazing. I wish I could hug you for this best explainantion. I learned a lot.
@bananaboydan3642
@bananaboydan3642 11 ай бұрын
How is the naive brute force any different from the fixed sliding window solution for a max subarray of size k
@alphapenguin9748
@alphapenguin9748 4 жыл бұрын
Please make more algorithm videos, you're literally the best explainer I have found on youtube.
@shashankshekhar5212
@shashankshekhar5212 3 жыл бұрын
Will it work if the input array has negative numbers [-1, -1, 1] and targetsum = 0
@MrAlmas01kz
@MrAlmas01kz 2 жыл бұрын
One of the best and easy explanations of this concept. thank you so much !
@syedaffanhameed14
@syedaffanhameed14 2 жыл бұрын
I found "Algorithmic mental models" is a great concept for solving problems please bring more videos like this.
@SantoshPrajapati-ps9nj
@SantoshPrajapati-ps9nj 6 ай бұрын
This is the best explaination that i have came across 🙂
@benzybert5113
@benzybert5113 2 жыл бұрын
amazing video! please add more mental algorithms (two pointer for example...)
@s.bamahfoodh
@s.bamahfoodh 11 ай бұрын
I don't understand where in the question it is asking for the subset to be sequential items. I mean why this subset is not the maximum [7, 8, 8]?
@rainMeToSleep
@rainMeToSleep 4 жыл бұрын
Hi @The Simple Engineer When are going to upload the videos on Trees and Graphs?
@sahanatalanki6884
@sahanatalanki6884 2 жыл бұрын
This algorithm will fail if the array contains negative value. Can you post a video with a solution considering negative elements in the array?
@TheDev05
@TheDev05 3 жыл бұрын
The animation for second technique: dynamic SW is just awesome, I loved it
@Ash-fo4qs
@Ash-fo4qs 2 жыл бұрын
Graph, DP, Greedy, BFS-DFS, mental models plz.
@msamogh96
@msamogh96 2 жыл бұрын
31:14 You'll have to add 1 even if the arrays weren't 0-indexed. Adding 1 has nothing to do with the indexing.
@CEOofTheHood
@CEOofTheHood 3 жыл бұрын
Please, If you are too busy to make videos will you please tell me a resource where I can learn these mental models. I am grinding leetcode and I get stuck on every new question. Please help.
@vincenthou6459
@vincenthou6459 2 жыл бұрын
This is such an awesome video by abstracting the idea of sliding window to tackle a group of specific issues with similarities. Regarding the question of smallest subarray for a given target, I think we need to add the constraint that every element in the array is positive. Otherwise, the sum can be reduced even if we grow the size of the window without dropping the front one. 28:37
@sierraobi311
@sierraobi311 Ай бұрын
Please do! I want to recommend them to all my friends.
@jm56481
@jm56481 2 жыл бұрын
I don't comment much but this video is a triumph lol. Great job on the explanation Ryan.
@emilmohaneriksson
@emilmohaneriksson 2 жыл бұрын
Perhaps an extra condition windowstart
@glaze4629
@glaze4629 Жыл бұрын
Great video. I wonder which intellij theme is thath? It's very pretty
@alxx736
@alxx736 2 жыл бұрын
Amazing, can you tell me the software for drawing?
@amangautam2792
@amangautam2792 4 жыл бұрын
Thanks for such nice explanation!
@MrPrav6
@MrPrav6 3 жыл бұрын
the dynamic windows example code fails for below test case: int[] nums = new int[] { 9, 4, 2,2}; //Act int actual = minSubarray.GetSmallestSubarray(nums, 8); int expected = 3; it will return 1.
@victorklimov3175
@victorklimov3175 Жыл бұрын
Thank you for GREAT explanation!
@ripperx444
@ripperx444 9 ай бұрын
Great video but Java blows ... Python is way easier to learn this
@cocoarecords
@cocoarecords 3 жыл бұрын
wouldnt mind buying a algorthmic mental models course from you tbh man
@leonardo_magallanes
@leonardo_magallanes 2 жыл бұрын
Thank you so much for this video, I helped me a lot to understand this concept better.
@darshantawte7435
@darshantawte7435 4 жыл бұрын
Pls make such videos on other important algorithmic models
@sandeepk9640
@sandeepk9640 2 жыл бұрын
Thank You.. for demystifying 😍
@andrewgraham3119
@andrewgraham3119 Жыл бұрын
Great explanation. It really helped me to fully understand
@Mop_Deep
@Mop_Deep 2 жыл бұрын
It's been three years since you dropped this video. I wish you would cover the other patterns.
@TheSimpleEngineer
@TheSimpleEngineer 2 жыл бұрын
What patterns are you interested in?
@Mop_Deep
@Mop_Deep 2 жыл бұрын
@@TheSimpleEngineer Like the ones in "Grokking the Coding Interview: Patterns for Coding Questions". You explained this to me so well. Would be dope if you did that for patterns from that list. i like your teaching style my friend. I would pay money if you had a course explaining those.
@HologramJay
@HologramJay 2 жыл бұрын
@@TheSimpleEngineer Hi, Ryan. Hope you can clear something up for me. The solution for smallestSubArray isn't O(n^2) because you aren't looping over the array in the while loop, correct? You're just looping until the condition is no longer true. Is that right, or am I totally off?
@klaaskabini5407
@klaaskabini5407 2 жыл бұрын
Best explanation so far. Please make more videos on algorithmns
@bestinbabu4244
@bestinbabu4244 14 күн бұрын
pls pls pls make more videos i beg you bro
@jobhunter-j7d
@jobhunter-j7d 10 ай бұрын
Longest substring length with K distinct chars int longestSubWithKDistinct(vector &A, int k){ int l = 0, r = 0; int ans = INT_MIN; unordered_map m; while (r < A.size()) { m[A[r]]++; while(m.size() > k){ m[A[l]]--; if(m[A[l]] == 0) m.erase(A[l]); l++; } ans = max(ans, r - l + 1); r++; } return ans; }
@CantBeTamed53
@CantBeTamed53 2 жыл бұрын
Great use of graphics to explain the algorithm! :D
@leandrormor
@leandrormor 3 жыл бұрын
I want learn more algorithmic mental models :) thanks for shaing
@vishwable
@vishwable 4 жыл бұрын
Very beautiful Video...need more such videos.
@isaidspaghetti
@isaidspaghetti 8 ай бұрын
Amazing video thank you friend!
@monsterhoodlum
@monsterhoodlum Жыл бұрын
Man i found you sooner. It would have been so much better.
@mriduljain1981
@mriduljain1981 Жыл бұрын
Doesnot it looks like english version of aditya verma xD
@ethanwu7338
@ethanwu7338 2 жыл бұрын
Dude, thank you so much for making those videos!
@downnloadz4483
@downnloadz4483 3 жыл бұрын
Amazing tutorial, thanks buddy
@equaco
@equaco 5 жыл бұрын
Awesome!Please post up the code/links soon!
@TheSimpleEngineer
@TheSimpleEngineer 5 жыл бұрын
Added! Link in description
@dolicious
@dolicious 3 жыл бұрын
this was such a good explanation !! I hate Java but through your explanation I was able to understand what each line of code was doing. Even pointing out certain words that give away how one should solve the problem. Looking forward to future algo breakdowns !
@alivation3409
@alivation3409 2 жыл бұрын
Same! I know this is an old comment, but I was able to mentally translate this into the programming languages I do know algorithms in. This is a great video.
@abhiachalla4165
@abhiachalla4165 Жыл бұрын
Your variable names are enough to explain the code to a naive student
@nirmalk55
@nirmalk55 2 жыл бұрын
Damn goood... Best video on this subject !!
How DNS Works Visually
10:46
Ryan Schachte
Рет қаралды 77 М.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 554 М.
OYUNCAK MİKROFON İLE TRAFİK LAMBASINI DEĞİŞTİRDİ 😱
00:17
Melih Taşçı
Рет қаралды 12 МЛН
WORLD BEST MAGIC SECRETS
00:50
MasomkaMagic
Рет қаралды 55 МЛН
Cute
00:16
Oyuncak Avı
Рет қаралды 12 МЛН
Whiteboard Coding Interviews: 6 Steps to Solve Any Problem
15:18
Fullstack Academy
Рет қаралды 371 М.
Most Common Concepts for Coding Interviews
6:08
NeetCode
Рет қаралды 318 М.
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 236 М.
IPv4, CIDR, and VPC Subnets Made Simple!
23:47
Ryan Schachte
Рет қаралды 197 М.
The 5 String Interview Patterns You Need to Know
10:49
Byte by Byte
Рет қаралды 91 М.
you will never ask about pointers again after watching this video
8:03
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 379 М.