Detonate the Maximum Bombs - Leetcode 2101 - Python

  Рет қаралды 12,553

NeetCodeIO

NeetCodeIO

Күн бұрын

Solving Leetcode 2101 - Detonate the Maximum Bombs, today's daily leetcode on June 1.
🚀 neetcode.io/ - A better way to prepare for Coding Interviews
🥷 Discord: / discord
🐦 Twitter: / neetcode1
🐮 Support the channel: / neetcode
⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 -...
💡 DYNAMIC PROGRAMMING PLAYLIST: • House Robber - Leetco...
Problem Link: leetcode.com/problems/detonat...
0:00 - Read the problem
0:35 - Drawing Explanation
6:12 - Coding Explanation
leetcode 2101
#neetcode #leetcode #python

Пікірлер: 37
@aq6910
@aq6910 Жыл бұрын
Hopefully you don’t get raided by the fbi after they read the title
@MP-ny3ep
@MP-ny3ep Жыл бұрын
Great explanation as always ! Thank you
@Pegasus02Kr
@Pegasus02Kr Жыл бұрын
Great lecture sir. I couldn't even think of graph solution alone.
@mohithadiyal6083
@mohithadiyal6083 Жыл бұрын
It's the best explanation ever
@kartikeyrana3736
@kartikeyrana3736 Жыл бұрын
funny thing is, i brute forced my way through this problem once i saw bombs.length
@Frank-pg7xx
@Frank-pg7xx 8 ай бұрын
ln 5 and 6 to create none duplicate pairs while taking out when i == j is magic. It makes sense when you see it however in an interview I wouldn't be able to find this solution.
@suneosama939
@suneosama939 6 ай бұрын
Cna anhbody explain dfs do you write line 15 first or line 23 first?
@kb-ru4md
@kb-ru4md Ай бұрын
Dont watch in airport:p awesome videos
@AMX0013
@AMX0013 10 ай бұрын
I want to solve this with union find but failed for a testcase. Base idea being whenever ther is an edge, we create an union. And based highest ranked component is the max bombs detonated. I tried with chatgpt to understand why its not possible but im not satisfied yet. Can you please help me with this itch!!!
@working7343
@working7343 9 ай бұрын
if getting error on 162 test case, ceil() the distance calculated ;)
@uptwist2260
@uptwist2260 Жыл бұрын
I fell into a trap of trying to cache the number of visited nodes for each node but it didn't work. I figured out why in the end but I had a very difficult time understanding why. If you want to know, lets say we have node 0 mapping to node 1 and 2. And we have node 1 mapping to node 0. 0 => [1, 2] 1 => [0] If we recursively go through node zero, we would cache 0 => 3, 1 => 1, and 2 => 1. The cache meaning `node => number of visited nodes`. After node 0, If we start recursively going through node 1, we would check our cache and see that node 1 only visits 1 node, and then return 1, WHICH IS INCORRECT, because node 1 maps to node 0 and node 0 maps to 1 other node. This was annoying for me to wrap my hand around at first but yeah, caching in this problem does not work. I tried other ways of caching also, but they did not work as well for a similar reason. My intuition from the start was that maybe you can also solve this using union find, but I have not tried it yet. If anyone knows if it works or doesn't, please let me know. edit: union find will not work. quoting from discussions: `will not work.consider a small circle in range of two big circles.but both big circles not in range of each other.union find gives 3 but correct answer is 2 .because a big circle can detonate a small circle but small circle cant detonate the other big circle` also according to discussions and google: `Union-Find is NOT applicable on DIRECTED Graphs`. oops.
@mahimanzum
@mahimanzum Жыл бұрын
This comment should be pinned as a top comment. Thanks
@deadlyecho
@deadlyecho Жыл бұрын
Very good mate.. 👍
@pranshu_awasthi
@pranshu_awasthi Жыл бұрын
Can we do a workaround here , where the cache value of all the nodes in a strongly connected component is equal. For eg. 1 -> 2,3 2 -> 3 -> 2, 4 4 -> 2, 3, 5 5 -> 2, 3, 4 If we start traversing from 3 /4 / 5 , we'd be able to cover 4 nodes , so we'll naturally cache them as 4 But , we'll also cache 2 -> 4 because that does not mean we can move to 3 other nodes from 2 but that would mean that 2 is a part of a component in which 4 nodes are present. This way , when we start the traversal from 1 , and move to 2 It'll get the cache as 4 & return. Would this work ? Any suggestions ?
@ihsannuruliman4005
@ihsannuruliman4005 7 ай бұрын
It's simple, dp won't work if there's no recurrence relation. you have to determine if it's possible to get the value of a node without traversing. in that the node has to have all the information needed to get the value directly. in this case it can't, because you need information about the nodes that are already visited before going to this node. You can try hard to also cache the visit set as well (for example using bitmasking), but performance speaking will not change the overall TC. I never think about using DP for this kind of problem. The concept is clear once you really understand dp.
@sumanshukumarshaw8157
@sumanshukumarshaw8157 Жыл бұрын
I love your explanations ,can you Please solve the problem "2127. Maximum Employees to Be Invited to a Meeting" please!!!!:)
@Walid-Sahab
@Walid-Sahab 10 ай бұрын
bset explanation 🌟
@YT.Nikolay
@YT.Nikolay Жыл бұрын
ok, I watched till 3:15 and then I solved the problem by myself, but graph... I would not solve it without a hint, I was thinking this is an interval problem, or heap
@michaelharris1305
@michaelharris1305 9 ай бұрын
interval problems are generally lies on x axis
@mathew5659
@mathew5659 Жыл бұрын
Great solution! Can we use disjoint sets for this problem? as we're constructing the graph from nothing
@16avikasgupta70
@16avikasgupta70 Жыл бұрын
Ya even I was thinking the same because it can be thought of finding the max number of nodes in connected component but the problem is how we are going to apply dsu
@umeshhbhat
@umeshhbhat 5 ай бұрын
No, we ca not as the graph wont be bidirectional. For eg: if A can detonate mean does not necessarily mean that B can detonate A
@YT.Nikolay
@YT.Nikolay Жыл бұрын
this problem is crazy >_
@lakshmanvengadesan9096
@lakshmanvengadesan9096 Жыл бұрын
Understanding that this is a graph problem is the hard part
@ankurgoswami4441
@ankurgoswami4441 Жыл бұрын
if u know it is graph problem, after that it is pretty easy, textbook DFS
@yolo3659
@yolo3659 Жыл бұрын
I do not understand how the time complexity is O(n^3). For every starting node you will at max visit every other node exactly once in a dfs. So the time complexity is O(n^2).
@hemanth.alluri
@hemanth.alluri Жыл бұрын
It's because you have to DFS for every node in the graph.
@hemanth.alluri
@hemanth.alluri Жыл бұрын
And DFS itself takes O(n^2) time.
@user-le6ts6ci7h
@user-le6ts6ci7h 8 ай бұрын
Nope the time complexity is o(n^2) no matter what you are not going to visit more than n node for a start jode
@DrSilvey
@DrSilvey Жыл бұрын
AMBATABLOW
@champion5946
@champion5946 Жыл бұрын
For C++ Coders Maybe getting OverFlow (Because I Was Getting 😥😥) While Calculating DISTANCE for(int i = 0; i < n; ++i) { long long x1 = bombs[i][0], y1 = bombs[i][1], r1 = bombs[i][2]; for(int j = i + 1; j < n; ++j) { long long x2 = bombs[j][0], y2 = bombs[j][1], r2 = bombs[j][2]; double distance = sqrt((long long)((x1 - x2) * (x1 - x2)) + (long long)((y1 - y2) * (y1 - y2))); if(distance
@Btcrock00
@Btcrock00 Жыл бұрын
Don't calculate the root instead compare it with square of radius.
@suneosama939
@suneosama939 7 ай бұрын
Why return 0 if i in visit? I don't understand
@binfos7434
@binfos7434 6 ай бұрын
You can return `None` if you want. He returned `0` because he may have wanted to use a counter but then decided not to.
@sushantsrivastav
@sushantsrivastav Жыл бұрын
I was asked this question in an interview yesterday. I am not making this up.
@johnnychang3456
@johnnychang3456 Жыл бұрын
Man I can't imagine being given this problem during interview. My brain would black out completely.
@venky3639
@venky3639 Ай бұрын
How tf u got the intuition to solve it with a graph 😢
Time Needed to Inform All Employees - Leetcode 1376 - Python
10:42
Wait for the last one! 👀
00:28
Josh Horton
Рет қаралды 147 МЛН
Me: Don't cross there's cars coming
00:16
LOL
Рет қаралды 14 МЛН
МАМА И STANDOFF 2 😳 !FAKE GUN! #shorts
00:34
INNA SERG
Рет қаралды 4,7 МЛН
The child was abused by the clown#Short #Officer Rabbit #angel
00:55
兔子警官
Рет қаралды 24 МЛН
Dota2 Senate - Leetcode 649 - Python
14:48
NeetCodeIO
Рет қаралды 18 М.
Number of Enclaves - Leetcode 1020 - Python
11:39
NeetCodeIO
Рет қаралды 7 М.
First Bad Version - Leetcode 278 - Binary Search (Python)
8:12
Greg Hogg
Рет қаралды 1,1 М.
8 patterns to solve 80% Leetcode problems
7:30
Sahil & Sarra
Рет қаралды 230 М.
Count Ways to Build Good Strings - Leetcode 2466 - Python
14:09
New 21 Game - Leetcode 837 - Python
19:03
NeetCodeIO
Рет қаралды 11 М.
Arranging Coins - Leetcode 441 - Python
12:53
NeetCode
Рет қаралды 33 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 614 М.
Wait for the last one! 👀
00:28
Josh Horton
Рет қаралды 147 МЛН