LeetCode Day 17 - Number of Islands (Grid BFS Algorithm)

  Рет қаралды 60,873

Errichto Algorithms

Errichto Algorithms

Күн бұрын

Пікірлер: 104
@dscmtr686
@dscmtr686 4 жыл бұрын
Small improvement: you can mark field as visited by seting grid to '0', then you don't need matrix visited.
@sananyuzb
@sananyuzb 4 жыл бұрын
That's possible if you can change the input. If you are not allowed to change the input, you should use visited array. It is good practice to ask that type of question during interview, so you can construct correct solution.
@Errichto
@Errichto 4 жыл бұрын
Cool idea!
@liwaiyip1769
@liwaiyip1769 4 жыл бұрын
@@sananyuzb I think it is ok to go for the solution with changing the input. You can simply say that you can make a clone of the input if they do not allow changing the input. That still costs O(H*W) space. But if they allow changing the input, it will improve the space complexity.
@fauzanwaseem6436
@fauzanwaseem6436 3 жыл бұрын
Nice idea bro
@Errichto
@Errichto 4 жыл бұрын
If you participate in competitions, this is a busy weekend. Here are some reminders for you. Today (Saturday) is Topcoder Open 1A codeforces.com/blog/entry/76067 Tomorrow (Sunday) is Google Code Jam 1B codingcompetitions.withgoogle.com/codejam EDIT: and Google Kickstart in a few hours, thanks for pointing this out codingcompetitions.withgoogle.com/kickstart
@ebby41
@ebby41 4 жыл бұрын
Today I solved this problem using DFS all by myself for the first time ever. I want to thank you for being such a great source of knowledge. I promise that if I become as good as you, I will fly all the way down to Poland to personally thank you.
@AnkitJosh
@AnkitJosh 3 жыл бұрын
Ehhh keep going champ!
@AAAAAA-gj2di
@AAAAAA-gj2di 3 жыл бұрын
Hello please can anyone tell me why dfs or bfs is implied to count those
3 жыл бұрын
@@AAAAAA-gj2di because with dfs/bfs you will get to all the other possible "1"s from the starting "1". If after a bfs cycle a "1" hasn't been visited, then it is not a neighbor of any of the already visited "1"s.
@AAAAAA-gj2di
@AAAAAA-gj2di 3 жыл бұрын
@ suppose I assume a "1" as my root node....then which "1"s are it's children?
@AAAAAA-gj2di
@AAAAAA-gj2di 3 жыл бұрын
@ sorry mate i'm very new to coding
@mariansoltan1318
@mariansoltan1318 4 жыл бұрын
I really like problems including DFS or BFS because these are the ones that are really making sense to me and for me it's a pleasure to solve them
@arinroday302
@arinroday302 4 жыл бұрын
The best part is that debugging is also too hard
@ChandraShekhar-by3cd
@ChandraShekhar-by3cd 4 жыл бұрын
@Errichto Please Continue The LeetCoder Series as it really helped us in grasping the basic as well as advanced concept to tackle any coding problems. Please take it as a Request on behalf of all the Coding Community. We need this kind of seried to continue.....
@ChandraShekhar-by3cd
@ChandraShekhar-by3cd 4 жыл бұрын
Hi Errichto , Can you please Upload a video on MO's ALGORITHM as in competitive programming it is being used extensively and I am not able to find any useful resource on that. Thanks
@vigneshsekar2001
@vigneshsekar2001 4 жыл бұрын
Yes Errichto pls do
@suprabhatkumar151
@suprabhatkumar151 4 жыл бұрын
Checkout this video and other related to MO's algo in the playlist kzbin.info/www/bejne/babRlY2CdpiSqLc
@ChandraShekhar-by3cd
@ChandraShekhar-by3cd 4 жыл бұрын
@@suprabhatkumar151 Thanks a lot for the link..it really gives all the idea from basics to understand it more deeply.
@SamDaBest
@SamDaBest 4 жыл бұрын
This problem is the first question I solve just after I learned dfs, I was so happy to solve this at that time.
@anuragpatro8979
@anuragpatro8979 4 жыл бұрын
Hey, do you mind telling how you used DFS? BFS makes sense as you check all adjacent ones for the same island, how would you go about DFS?
@joshuaclark9425
@joshuaclark9425 4 жыл бұрын
@@anuragpatro8979 The only difference is you add all the neighbouring land cells to a stack instead of a queue. So it explores as far as it can in one direction first rather than spreading out evenly from the starting point.
@israkulhaquetusher3256
@israkulhaquetusher3256 4 жыл бұрын
@@anuragpatro8979 you have to run N dfs if there are N island ( the idea behind is flood fill )
@lovvyparhar393
@lovvyparhar393 4 жыл бұрын
Exactly same thing happened to me
@anuragpatro8979
@anuragpatro8979 4 жыл бұрын
@@joshuaclark9425 Got it, thanks
@subarukun8001
@subarukun8001 4 жыл бұрын
WOW, your solution is clean and understandable.
@abhishekpadamwar2333
@abhishekpadamwar2333 4 жыл бұрын
Can you make a video explaining union find approach?
@leetcodesolver9092
@leetcodesolver9092 4 жыл бұрын
Nice explanation. I solved this problem using DFS.
@shubhamk840
@shubhamk840 4 жыл бұрын
i am new programmer , i solve problems from many coding sites and many a times I solve problems that are authored by errichto. for example on hackerrank named lisa's workbook ,
@raghurrai
@raghurrai 4 жыл бұрын
Amazing! Thanks for this wonderful explanation. Really loved the refactoring techniques
@worklifeandstuff
@worklifeandstuff 4 жыл бұрын
I enjoyed it too, mate!!!
@raghurrai
@raghurrai 4 жыл бұрын
@@worklifeandstuff hue hue
@nopethisisnotreal1434
@nopethisisnotreal1434 4 жыл бұрын
You make every question seem easy thank you for your help!
@antonyfil
@antonyfil 4 жыл бұрын
This problem came from computer graphics - filling closed areas with some color. All flood fill algorithms are applicable here. en.m.wikipedia.org/wiki/Flood_fill
@harshpatel105
@harshpatel105 4 жыл бұрын
Ah thanks!
@omerronen2329
@omerronen2329 4 жыл бұрын
Could you please post a quick tutorial on how to set up the online judge for code jam tomorrow?
@kevinhwa
@kevinhwa 4 жыл бұрын
Can you make a video on DSU for this problem?
@pritamrao3256
@pritamrao3256 4 жыл бұрын
Refer code n code youtube channel he has a wonderful playlist on that topic
@omarnader686
@omarnader686 4 жыл бұрын
@Errichto can you please explain the solution using Disjoint set union???
@CodingWithUnity
@CodingWithUnity 4 жыл бұрын
Cool series, enjoying it!
@FazeelUsmani
@FazeelUsmani 4 жыл бұрын
Great Idea!! of taking all the directions in a vector of pairs. Now, I can apply this same trick to some backtracking problems like N Queens, Rat in a Maze, Sudoku and much more. Thanks! Errichto. #cleancode What if you solve these Leetcode challenge problems on a live stream? I mean you'll get more familiar to code while handling the crowd and also we'll gain enough experience of problem-solving. I love the way you think when one thing goes wrong you try out every other possible way. And finally, get it done. :)
@guy491
@guy491 4 жыл бұрын
@Errichto Would love to see the DSU solution as well, or a video on that algorithm! I used a DSU solution for this but my implementation was quite naive (no path compression, splitting etc.). It would be cool to see the implementation you use for competitions.
@namanchetwani2028
@namanchetwani2028 Жыл бұрын
You are literally amazing.😶
@climbnexplore1187
@climbnexplore1187 4 жыл бұрын
Thanks for the hardcoding tip!!
@dhrubankadutta2245
@dhrubankadutta2245 4 жыл бұрын
Can anyone provide a good resource online to learn graph, tree and heaps? I always find that every series misses some part or the other.
@randomrandom316
@randomrandom316 4 жыл бұрын
The MIT lectures though a little slow paced are quite good for the fundamentals. kzbin.info/www/bejne/fqW2pnRuZbaHr9E
@dhrubankadutta2245
@dhrubankadutta2245 4 жыл бұрын
@@randomrandom316 Thanks
@ivanp4740
@ivanp4740 4 жыл бұрын
Robert Sedgewick and his algorithms course on Coursera The best course I have ever seen. Good structure, code examples(of any algorithm/data structure), visualization, proofs and properties of algorithms. He covers almost every topic which is important for coding interview
@deep_swamp
@deep_swamp 4 жыл бұрын
This is an awesome Python resource focused solely on mastering data structures: donsheehy.github.io/datastructures/
@andreykarayvansky9549
@andreykarayvansky9549 4 жыл бұрын
I solved this task using just DFS. I also wonder is union/find a must to know algorithm? I often find if a task can be solved using UF (DSU) there is always some other less academic way to solve it. I wonder what is your opinion. Thanks!
@ej9806
@ej9806 4 жыл бұрын
BFS is better than DFS because you won't get stack overflow
@andreykarayvansky9549
@andreykarayvansky9549 4 жыл бұрын
You can implement DSF with no stackoverflow issues
@andreykarayvansky9549
@andreykarayvansky9549 4 жыл бұрын
I actually used UF many times. I even remember I solved this task using UF. The problem is that if I want to use it (and that does not happen often) I need to look it up since this algorithm is quite easy to forget
@ambarishbanerjee414
@ambarishbanerjee414 4 жыл бұрын
How come your past code doesn't show up in the editor? If i have solved it before my past code shows up as soon as i click on the problem and it kind of spoils the interest to solve again.
@ihopethiscommentisntabusiv4670
@ihopethiscommentisntabusiv4670 3 жыл бұрын
@Errichto could you explain why this is linear runtime? Why is it not O(n^2) because you have the outer loops + the BFS traversal.
@rahul0000nm
@rahul0000nm 4 жыл бұрын
Could you please explain the code you start with? The one which helps you debug your solution locally
@ChandraShekhar-by3cd
@ChandraShekhar-by3cd 4 жыл бұрын
@Errichto What is the time complexity for this approach??
@catalingriu8817
@catalingriu8817 4 жыл бұрын
How to create a cpp file with my template from terminal? (when i type "geany a.cpp" it's emty)
@lavishgarg4274
@lavishgarg4274 4 жыл бұрын
kzbin.info/www/bejne/m4G9dp6Yl8tmnMU
@catalingriu8817
@catalingriu8817 4 жыл бұрын
@@lavishgarg4274He didn't talk about my question. I watched the video 3 times.
@Errichto
@Errichto 4 жыл бұрын
if a.cpp doesn't exist, geany opens an empty file I have file templates.cpp and I copy by hand from it, or type "cp templates.cpp a.cpp" in terminal
@sandipjana5553
@sandipjana5553 4 жыл бұрын
Hi Errichto.. at 0:45 you said it can be solved using DSU and then you said we need to know "some particle technique" . Can you mention the name of the technique once. Thanks in advance
@fauzanwaseem6436
@fauzanwaseem6436 3 жыл бұрын
Nice job bro 👍
@mohnishsatidasani7552
@mohnishsatidasani7552 4 жыл бұрын
How to Apply DSU over a matrix grpah without flatting the matrix or grid?
@i.m.anonymous8492
@i.m.anonymous8492 4 жыл бұрын
Respected Sir, I am unable to understand how the input output is working for the given question, i.e. how is output for example 1 = 1 and that off example 2 = 3.
@shaunbillonesshauntunado7277
@shaunbillonesshauntunado7277 4 жыл бұрын
Can you help me with a question in islanda as well?
@PavanKumar-jt5mq
@PavanKumar-jt5mq 4 жыл бұрын
I think a dfs written as lambda function would be a bit shorter implementation. I was expecting this 😅.
@rohitdatta6379
@rohitdatta6379 4 жыл бұрын
rather than using extra array you can just change the '1' to '0' to check!
@abdoulayediagne2976
@abdoulayediagne2976 4 жыл бұрын
Indeed! And then space complexity will be O(1)
@baukabaiorazov5277
@baukabaiorazov5277 4 жыл бұрын
I think we don’t need visited. We can change ‘1’ to ‘0’ without extra memory.
@mogovanjonathan
@mogovanjonathan 4 жыл бұрын
Hy Errichto, you posted 2 moths ago a video named "C++ Bitsets in Competitive Programming". Could you please help me with the c++ solution of the first problem (the one with the worker during 1 month) using popcount, and how to make 2 3 5 6 8 (it there were only 8 day) in 01101101, wich is in decimal 109? It would be very helpful! Thank you for reading this and answering!
@dfhfdhahhs9803
@dfhfdhahhs9803 4 жыл бұрын
I don't think 01101101 is decimal. Seems to me that they respond to 1,2,3,4,5,6,7,8 counting from left to right. 1 meaning it's there and 0 it's not 01101101 -> 0,2,3,0,5,6,0,8.
@mogovanjonathan
@mogovanjonathan 4 жыл бұрын
@@dfhfdhahhs9803and how to I change this 0,2,3,0,5,6,0,8. into 01101101 using bitset? thanks
@MatthewJDrake
@MatthewJDrake 4 жыл бұрын
What keyboard do you use? (Sorry if you get asked that a lot...)
@nitishraj-pj2ef
@nitishraj-pj2ef 4 жыл бұрын
Did by using DFS
@codeguy21
@codeguy21 4 жыл бұрын
Guys I have a doubt the real programming that we work as a software engineer is that easier than competitive programming,
@shashikantpunia9019
@shashikantpunia9019 4 жыл бұрын
depends on individual interest. However development is quite easy compared to cp
@vaibhavvashist238
@vaibhavvashist238 4 жыл бұрын
can you tell me how to get motivated and maintain consitency?
@deshabhaktg6530
@deshabhaktg6530 3 жыл бұрын
GOD❤️🔥😍
@musicfan1695
@musicfan1695 4 жыл бұрын
errichto do you drink coffee too often ?
@prakash_77
@prakash_77 4 жыл бұрын
As someone who doesn't know BFS,DFS or Find/Union(DSU), this solution went over my head. 😑
@Player-ub9tg
@Player-ub9tg 4 жыл бұрын
There is no way of solving this without that algos
@gamegamesoumic
@gamegamesoumic 4 жыл бұрын
I did it with DFS.
@mr.mystiks9968
@mr.mystiks9968 4 жыл бұрын
I feel like BFS and DFS problems are least likely to come up in interviews just because they’re all very similar and don’t require as much intellect as say Dynammic Programming or Greedy algorithms.
@akulsinghal216
@akulsinghal216 4 жыл бұрын
Well then you haven't solved good bfs/dfs problems.
@ivanp4740
@ivanp4740 4 жыл бұрын
Not agree, there is a lot of dfs/bfs problem that will struggle you a lot :) Especially the ones which combine different techniques For example this one: leetcode.com/problems/robot-room-cleaner/ which is a combination of DFS and backtracking. Can't say that this is a very hard problem(especially when you got a hint about backtracking :D) but anyway... Also can add that there is a lot of problem with DP that don't require a lot of intellect. Actually, a lot of dp questions can be solved manually. You just write brute force recursive solution, then you make a drawing and you will see that there is a lot of repeated calls, then you just use memorization(or bottom-up as memory optimization) . This is not hard but visualization is very important because repeated calls can be non-obvious(that's why it looks hard) About greedy - agree. For me, it's the hardest topic. Can't understand where this will work and where will not
@williambillemeyling5647
@williambillemeyling5647 4 жыл бұрын
Grid problems are my favorite graph problems! :)
@aimelodies5
@aimelodies5 4 жыл бұрын
One suggestion , please use white board it will be more intutative for us .
@dr.darkfurygaming9174
@dr.darkfurygaming9174 4 жыл бұрын
When you forget to upload video😑
@AlexandrBorschchev
@AlexandrBorschchev 3 жыл бұрын
Do I have impostor syndrome if i watch Errichto?
@John-Weak
@John-Weak 4 жыл бұрын
Dumb me used simple old recursion for this
@baibhabmondal1740
@baibhabmondal1740 4 жыл бұрын
lol same. Idk why but i keep going back to recursion, even for today's problem. :P
@John-Weak
@John-Weak 4 жыл бұрын
@@baibhabmondal1740 same energy bro,but dynamic is better for today's problem
@naveenrawat6278
@naveenrawat6278 4 жыл бұрын
First.
@BharatPatidar_juriz
@BharatPatidar_juriz 4 жыл бұрын
second xD
@mariansoltan1318
@mariansoltan1318 4 жыл бұрын
Seventh
LeetCode Day 18 - Grid Minimum Path Sum
4:28
Errichto Algorithms
Рет қаралды 30 М.
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 190 М.
She made herself an ear of corn from his marmalade candies🌽🌽🌽
00:38
Valja & Maxim Family
Рет қаралды 18 МЛН
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 707 М.
Winning Google Kickstart Round A 2020 + Facecam
17:10
William Lin (tmwilliamlin168)
Рет қаралды 10 МЛН
Number of Islands - Leetcode 200 - Graphs (Python)
11:01
Greg Hogg
Рет қаралды 13 М.
NUMBER OF ISLANDS - Leetcode 200 - Python
11:41
NeetCode
Рет қаралды 345 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 684 М.
How I Mastered Data Structures and Algorithms in 8 Weeks
15:46
Aman Manazir
Рет қаралды 150 М.
one year of studying (it was a mistake)
12:51
Jeffrey Codes
Рет қаралды 41 М.
Number of Islands - Breadth First Search (LeetCode)
14:25
AlgosWithMichael
Рет қаралды 17 М.
Breadth First Search (BFS): Visualized and Explained
10:41
Reducible
Рет қаралды 232 М.
Mathematics doesn't actually make any sense
13:37
Sheafification of G
Рет қаралды 48 М.
She made herself an ear of corn from his marmalade candies🌽🌽🌽
00:38
Valja & Maxim Family
Рет қаралды 18 МЛН