Тёмный

Algorithms: Solve 'Connected Cells' Using DFS 

HackerRank
Подписаться 267 тыс.
Просмотров 123 тыс.
50% 1

Опубликовано:

 

27 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 52   
@Secretzstolen
@Secretzstolen 5 лет назад
This is a fantastic and elegant solution. My first attempts at super difficult problems tend to be more verbose lol so it's great to see simpler solutions. My first language was JS, then I worked in Python & TypeScript. Watching these videos & reading your book is actually getting me more comfortable with Java which is great. TypeScript was 👌 as I'm now familiar with types, which really helps in strictly typed languages. Thank you for this video and all your work! 👏💯👩‍💻
@harrymuir6625
@harrymuir6625 4 года назад
OMG! This video is awesome, I'm software engineer and it's so difficult to me solve matrix problems like this one (I hope to not be only the one who feels like that wayt)
@sophiaatn5339
@sophiaatn5339 3 года назад
you're not at all.
@radsimu
@radsimu 7 лет назад
if you cannot destroy matrix, you can increment visited 1's and then change any non zero value back to 1
@athletics365
@athletics365 3 года назад
@Flynn Boston that was the weirdest plug I've ever seen
@Stewty1
@Stewty1 3 года назад
I am learning this for a coding interview, thank you for sharing this !
@srinivasnangunuri1313
@srinivasnangunuri1313 5 лет назад
Daummmnn. this has cleared me up of so many gaps related to solving matrix island problems!
@jasonlu3603
@jasonlu3603 3 года назад
Agreed, matrix problems are the toughest since they have so many components to solve together. LeetCode has a problem very similar.
@DadBodSwagGod
@DadBodSwagGod 6 лет назад
Wow...this is an amazingly useful video No joke, if I saw this in August of last year, I would be working at Google right now
@The.Dark.Panther
@The.Dark.Panther 4 года назад
Damn. That's a pity... :/
@codingwitharman5329
@codingwitharman5329 4 года назад
You can always re-apply, right??
@DadBodSwagGod
@DadBodSwagGod 4 года назад
So here’s the thing... This was written over a year ago I AM working at Google right NOW
@codingwitharman5329
@codingwitharman5329 4 года назад
@@DadBodSwagGod That's great man!!
@paulb6611
@paulb6611 2 года назад
Actually I found another optimization. Since the array is starting from the top left and moving to the bottom right, you actually don't need to check the cell to the left or up from it because every time you check it, it will already have been set to 0.
@lameow76
@lameow76 2 года назад
because of the recursive calls there can be cells down in the group whose up's or left's neighbor cells would be un-visited
@syedmuhammadzaeemhasankazm7740
@syedmuhammadzaeemhasankazm7740 4 года назад
Hey Gayle , your solution is so simple and awesome! Can you please tell me, what is the Big O of this code? And how could we have optimised it? (if optimisation was possible) Thanks in advance. From what I can understand, we could have perhaps used memoization to reduce extra steps. Am I right? Although I am not sure what would have been complexity then
@SundarRajansrgm
@SundarRajansrgm 4 года назад
should it be if (r != row && c != col) , AND instead of OR ? we just want to avoid that specific current position. Anyhow the code works because of matrix[row][column]= 0
@sampathkumar3405
@sampathkumar3405 3 года назад
that is what i thought..the elements vertically up and down, elements horizontal get skipped in that case right?
@oceanview-u8q
@oceanview-u8q 6 лет назад
matrix[row][column]=0 looks good to avoid unnecessary search again. boundary check is everywhere in examples i've seen(rat in maze or find bomb or find bike in xx company campus, find anything in multi array, etc) to avoid 'out of list'. any missing minor case check can be added always since the video is just to share idea. as long as interviewee can write this in whiteboard in less than 15 or 20 min, still good enough to pass cause 1 hr is too short to check everything anyway. =)
@radsimu
@radsimu 7 лет назад
no need for line 20
@abhishekkthakur1
@abhishekkthakur1 5 лет назад
Jeez, you are awesome. Period.
@caesar18fifa63
@caesar18fifa63 6 лет назад
Q1. What does int size mean? is int size the value stored inside a particular matrix entry? Q2. Is this implementation of DFS basically saying check all adjacent matrix entries to the starting entry. whichever adjacent entry has a one, then make it the new starting entry and do the same thing all over again?
@Secretzstolen
@Secretzstolen 5 лет назад
Size is the name of the variable and int is the variable type (integer)
@Secretzstolen
@Secretzstolen 5 лет назад
Re: Q2 - yes that's basically what the recursion is doing. Although it's also marking off the cells is has visited already to a zero, so that way there won't be any duplicate checking
@Alexander-bk6oy
@Alexander-bk6oy 2 года назад
any idea on how to prevent that diagonal check ?
@wasd3108
@wasd3108 2 года назад
6:40 there's literally no difference if you check at recursion start or before recursion start, how is it "so many more problems"?
@QiMU01
@QiMU01 4 года назад
This is awesome!
@shaunbillonesshauntunado7277
@shaunbillonesshauntunado7277 4 года назад
I need a help with disconnected islands, can you help me?
@balluvwdwadi8995
@balluvwdwadi8995 3 года назад
You can use recursion
@SpiritOfIndiaaa
@SpiritOfIndiaaa 7 лет назад
Need a bit more details when explaining the traversal of maxRegion
@narayanasai
@narayanasai 3 года назад
will it work for [[0,1],[1,0]]
@nickgoupinets
@nickgoupinets 4 года назад
Why do we need to check for "if (r != row || c != col) {"? We are setting matrix[row][col] to 0 anyways, so it won't matter technically, or I am off?
@morubalok
@morubalok 4 года назад
avoiding the current element
@SundarRajansrgm
@SundarRajansrgm 4 года назад
Also should it be if (r != row && c != col) , AND instead of OR ?
@alokesh985
@alokesh985 4 года назад
It won't matter since we're setting mat[row][col] to 0
@lokeshs9449
@lokeshs9449 4 года назад
Instead we make size=0
@learnersparadise7492
@learnersparadise7492 3 года назад
@@alokesh985 it would matter as size is 1
@muhammadmohibkhan3745
@muhammadmohibkhan3745 5 лет назад
wont the size be equal to 0 if there is no region (no 1 in grid)? then size and maxRegion would both be 0, whereas size would be greater than maxRegion in all other cases. So do we really need to compare size with maxRegion instead of directly assigning size?
@dunnithful
@dunnithful 2 года назад
size being greater than maxRegion in all other cases is not true. you can find a region of size 3, then find a region of size 7, then find a region of size 4. with your logic, size would always equal the last found region (4 in this case, which is less than 7). im sure you already know this by now but this should answer it for anyone else reading this thread in the future
@operationsus9117
@operationsus9117 3 года назад
Anyone know why this solution doesnt work with javascript?
@Stewty1
@Stewty1 3 года назад
would be great if you have a python version of the code too
@ONCE_AGAIN
@ONCE_AGAIN 4 года назад
Time complexity? O(n^4)?
@adikatz3501
@adikatz3501 7 лет назад
What will happen if row/column are equal to 0? I think we will get an exception...
@b.m.7033
@b.m.7033 6 лет назад
Yes, I think so. I needed to add valid row/column checks to the inside for loops.
@AlMan123xyz
@AlMan123xyz 4 года назад
The condition is already present in row 10
@imanbio
@imanbio 6 лет назад
# example: finding islands on a map in Python import numpy as np map1 = np.array([[0,0,0,1,1,0,0], [0,1,0,0,1,1,0], [1,1,0,1,0,0,1], [0,0,0,0,0,1,0], [1,1,0,0,0,0,0], [0,0,0,1,0,0,0]]) print(map1) def findIslands(inputMap): Islands = [] # the list of found islands and their nodes: [[nodeSet1], [nodeSet2], ...] mapRows, mapColumns = inputMap.shape for r in range(mapRows): for c in range(mapColumns): if (inputMap[r, c] == 1) & (not discovered([r, c], Islands)): # function discovered checks if the node [c,r] has been found before newPoint = [r, c] newIsland = discoverIsland(newPoint, inputMap) Islands.append(newIsland) return Islands def discovered(newPoint, Islands): discoveredBefore = False for i in range(len(Islands)): if newPoint in Islands[i]: discoveredBefore = True return discoveredBefore def neighborsFind(newPoint, inputMap): mapRows, mapColumns = inputMap.shape neighbors = [] for rw in range(max(newPoint[0] - 1, 0), min(newPoint[0] + 1, mapRows - 1) + 1): for cm in range(max(newPoint[1] - 1, 0), min(newPoint[1] + 1, mapColumns - 1) + 1): if inputMap[rw, cm] == 1: neighbors.append([rw, cm]) return neighbors def discoverIsland(newPoint, inputMap): island = [newPoint] currentIndex = 0 while currentIndex < len(island): neighbors = neighborsFind(island[currentIndex], inputMap) for i in range(len(neighbors)): if not neighbors[i] in island: island.append(neighbors[i]) currentIndex += 1 return island islands = findIslands(map1) islands
@apnihorrorduniya
@apnihorrorduniya 5 лет назад
Omg are you human?
@somerandomguy000
@somerandomguy000 3 года назад
no, she just a good communicator and person capable of copying from a book the solution of a problem
@IsmailHossain-ov5ir
@IsmailHossain-ov5ir 4 года назад
I think you should also use C++ to solve hackerrank problems.
@hippityhoppity657
@hippityhoppity657 4 года назад
it should be "r!=row && c!=column" not ||
@HanifCarroll
@HanifCarroll 4 года назад
I was looking at that and thought the same thing, but second guessed myself because she didn't correct that mistake with editing like she did the others. Thanks for this comment.
@abhishekbose6989
@abhishekbose6989 4 года назад
its correct
Далее
Top 7 Algorithms for Coding Interviews Explained SIMPLY
21:22
Исповедь / Мася
2:47:10
Просмотров 190 тыс.
Wait for it 😂
00:19
Просмотров 4,5 млн
DFS vs BFS, When to Use Which?
9:25
Просмотров 2,2 тыс.
Algorithms: Graph Search, DFS and BFS
11:49
Просмотров 956 тыс.
Algorithms: Memoization and Dynamic Programming
11:17
Просмотров 971 тыс.
LeetCode was HARD until I Learned these 15 Patterns
13:00
Data Structures: Solve 'Contacts' Using Tries
8:54
Просмотров 123 тыс.