Detonate the Maximum Bombs | DFS | BFS | GOOGLE | Leetcode-2101 | Live Code + Explanation 🧑🏻‍💻

Sdílet
Vložit
  • čas přidán 1. 06. 2023
  • This is the 29th Video on our Graphs Playlist.
    In this video we will try to solve a very good Graph Problem with amazing problem statement "Detonate the Maximum Bombs " (Leetcode - 2101)
    We will solve it using :
    1. DFS
    2. BFS
    I am promising you, this problem will become easy once you are done with this video.
    If you have been following my "Graph Concepts & Qns" playlist , then these Qns will become very easy. Find the Link for that below.
    We will do live coding after explanation and see if we are able to pass all the test cases.
    Problem Name : Detonate the Maximum Bombs
    Company Tags : GOOGLE
    My solutions on Github : github.com/MAZHARMIK/Intervie...
    Leetcode Link : leetcode.com/problems/detonat...
    My DP Concepts Playlist : • Roadmap for DP | How t...
    My Graph Concepts Playlist : • Graph Concepts & Qns -...
    My GitHub Repo for interview preparation : github.com/MAZHARMIK/Intervie...
    Subscribe to my channel : / @codestorywithmik
    Instagram : / codestorywithmik
    Facebook : / 100090524295846
    ╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
    ║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
    ╠╗║╚╝║║╠╗║╚╣║║║║║═╣
    ╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
    #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips
    #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook

Komentáře • 57

  • @mrinalmadhav8119
    @mrinalmadhav8119 Před rokem +15

    Ur content is very good , please continue ur work

  • @shaamidrees6036
    @shaamidrees6036 Před rokem +8

    Wow brother no one explains like you

  • @wearevacationuncoverers
    @wearevacationuncoverers Před rokem +3

    You have a gifted talent to make Qns look easy. The way you explain is just unmatchable.

  • @mohammadsaquibabbas1152
    @mohammadsaquibabbas1152 Před rokem +4

    MIK Sir this video rocks...Best Video Intitutions was very awesome 😃

  • @nishantdehariya5769
    @nishantdehariya5769 Před 3 měsíci +1

    very nice explaination

  • @user-ub2is4rs4x
    @user-ub2is4rs4x Před 8 měsíci

    This channel is unbelievably good. I am shocked very few people know this legend. Let’s make this guy famous 🔥🔥🔥

  • @AnandKumar-kz3ls
    @AnandKumar-kz3ls Před rokem +2

    great explanation one thing to add for each node there may be (n-1) edges ( i.e Complete graph) in worst case so dfs would take n^2 time and we need to run dfs for each node so time complexity would be n^3 in worst case

  • @souravjoshi2293
    @souravjoshi2293 Před rokem +1

    You stand alone. No one explains like you

  • @anuppatankar4294
    @anuppatankar4294 Před rokem +3

    Great Video as always ❤

  • @indranilmajee5453
    @indranilmajee5453 Před 11 měsíci +1

    very nice explanation

  • @mohithadiyal6083
    @mohithadiyal6083 Před rokem +1

    The best😊

  • @vaibhavgupta973
    @vaibhavgupta973 Před rokem +1

    thank you !

  • @Thriftinghai
    @Thriftinghai Před rokem

    One of the best explanations

  • @tutuimam3381
    @tutuimam3381 Před rokem +1

    Thanks a lot

  • @doomhead9109
    @doomhead9109 Před rokem +1

    Thanks

  • @JJ-tp2dd
    @JJ-tp2dd Před rokem +5

    Java implementation:
    BFS:
    class Solution {
    private int BFS(Map adj, int i) {
    Queue q = new LinkedList();
    Set visited = new HashSet();
    q.offer(i);
    visited.add(i);
    while(!q.isEmpty()) {
    int u = q.peek();
    q.poll();
    //if(!adj.containsKey(u)) return visited.size();
    for(int v : adj.getOrDefault(u, new ArrayList())) {
    if(!visited.contains(v)) {
    q.offer(v);
    visited.add(v);
    }
    }
    }
    return visited.size();
    }
    public int maximumDetonation(int[][] bombs) {
    Map adj = new HashMap();
    int n = bombs.length;
    //build the graph
    for(int i=0; ij
    adj.computeIfAbsent(i,k->new ArrayList()).add(j);
    }
    }
    }
    int result = 0;
    //from each node find the maximum nodes that can be visited
    for(int i = 0; inew ArrayList()).add(j);
    }
    }
    }

    int answer = 0;
    //from each node find the maximum nodes that can be visited
    for(int i=0; i

  • @factsmadeiteasy9943
    @factsmadeiteasy9943 Před 3 měsíci +2

    There is similar question asked in Google interview but instead of bomb they gave routers and devices but concept is same

  • @YashSinghal
    @YashSinghal Před rokem +2

    4k :)

  • @vineettanwar7880
    @vineettanwar7880 Před rokem +1

    really good explanation thanks

  • @dheerajkumarjha150
    @dheerajkumarjha150 Před rokem +1

    Bhaiyaaaaaa aap ek baar topic wise divide kar do na is playlist ko aapka explanation bada accha lagta hai but ek baar topic wise playlist bana doge to accha rahega

    • @codestorywithMIK
      @codestorywithMIK  Před rokem +1

      Hi Dheeraj,
      If you check my playlist section,
      There are topic wise different playlists.
      I am sure that will help ❤️❤️❤️

    • @dheerajkumarjha150
      @dheerajkumarjha150 Před rokem

      @@codestorywithMIK yes got it bhaiya... Thank you so much 🙏❤️👑

  • @ugcwithaddi
    @ugcwithaddi Před rokem

    Gold content

  • @codeandtalk6
    @codeandtalk6 Před rokem +1

  • @SS-rt3fq
    @SS-rt3fq Před rokem +1

    Let circle1 => x1=2, y1=2, r1=2
    and circle2 => x2=5, y2=2, r2=2.
    Distance between centres: sqrt(3*3 + 0) = 3
    Here, r1 (2

    • @codestorywithMIK
      @codestorywithMIK  Před rokem

      Note that the detonation doesn’t take place if the circle intersects.
      A bomb (say B1) will detonate another bomb (say B2) only if the coordinate of centre of the bomb B2 lies inside the range of Bomb B1

    • @SS-rt3fq
      @SS-rt3fq Před rokem

      @@codestorywithMIK Got it. Thanks.

  • @samaulhaquemalik4600
    @samaulhaquemalik4600 Před rokem

    class Solution {
    public:
    int dfs(int root,vector&visited,vector&adj){
    visited[root] = true;
    int result = 0;
    for(int node : adj[root]){
    if(!visited[node])result += dfs(node,visited,adj);
    }
    return 1+result;
    }
    int maximumDetonation(vector& bombs) {
    vectoradj;
    int n = bombs.size();
    for(int i=0;i

  • @sidharthdhiman4522
    @sidharthdhiman4522 Před rokem +1

    sir one doubt , i made a unidirected graph , jaise example me b1 and b2 agr b2 b1 ke andar arha ahi toh b1 wil destroy b2 and if b2 radius is small he won't be able to detonate b1 but b2 is already inside b1 there is some common part so we can't say that b2 will destroy b1 also ?

    • @codestorywithMIK
      @codestorywithMIK  Před rokem +3

      Hi Sidharth,
      Any bomb (say Bomb1) will be destroyed by other bomb (Bomb2)
      IF,
      The Bomb1 comes INSIDE or ON the radius of Bomb2

    • @sidharthdhiman4522
      @sidharthdhiman4522 Před rokem +1

      @@codestorywithMIK got it sir

  • @ashutoshpandey1639
    @ashutoshpandey1639 Před rokem

    Hi everyone can someone tell me why if we find distance btw two centers and store it in long long and compare it with radius ( radius >= dist ) it fails for the second last test case , but if we use double to store distance it gives the correct output can someone tell me why it happens

    • @Coding4211
      @Coding4211 Před rokem +1

      Bc sqrt will be calculated in double and if you turn it to long long some values will be lost which are after the decimal point . Just like floor divison. This will give wrong answer.

    • @ashutoshpandey1639
      @ashutoshpandey1639 Před rokem

      @@Coding4211 mitr hm to radius >= dist le rhe to jade se jade dist km hoke radius ke equal ho jaega tb v to condition true h correct me if I'm wrong?

    • @Coding4211
      @Coding4211 Před rokem

      Yhi toh problem h equal nhi ana h. Distance agr jyada h toh usse km karoge toh answer increase nhi hojayega.

    • @Coding4211
      @Coding4211 Před rokem

      The bombs which should not be included will also come in the range

    • @ashutoshpandey1639
      @ashutoshpandey1639 Před rokem

      @@Coding4211 agar radius > dist use kre (dist ko double lene ke baad v) to ye nhi work krega fine? To Maan lo agar mera dist 7.8069 aa rha and radius 7 hai to agar mai dist ko long long me v lelu tb v to wo atleast radius ek equal hoga

  • @tauquirahmed1879
    @tauquirahmed1879 Před rokem +1

    taking the sqrt to calculate the distance is giving a wrong answer. I also tries Manhatan Distance. It is also giving a wrong answer. But I tries a different approach to calculate number of connected nodes using DFS. Can you please tell me where I went wrong.
    ```
    class Solution {
    public:
    int cnt = 0;
    void DFS(int node, unordered_map &adj, vector &visited)
    {
    visited[node]=1;
    cnt++;
    for(auto child: adj[node])
    {
    if(!visited[child])
    DFS(child, adj, visited);
    }
    }
    int maximumDetonation(vector& bombs) {
    int n = bombs.size();
    unordered_map adj;
    vector visited(n, 0);
    for(int i = 0; i

    • @codestorywithMIK
      @codestorywithMIK  Před rokem

      Hey Tauquir.
      You missed to reinitialize the visited array for other nodes after you call DFS for a node.
      Just add
      visited = vector(n, 0);
      After your DFS call.
      It will work like a charm

    • @tauquirahmed1879
      @tauquirahmed1879 Před rokem +1

      @@codestorywithMIK ohhhk.... Thanks a lot ❣️

  • @prajwalshaw9217
    @prajwalshaw9217 Před rokem

    Hello sir. I was facing a problem in this question. Actually my code is passing 161/162 test cases...don't know why the last test case is failing. Can you kindly help me point out the mistake in my code. Thanks for a wonderful explanation though:)
    void dfs(int&node,vectoradj[],vector&visited,int&c)
    {
    visited[node]=true;
    c++;
    for(auto &it : adj[node])
    {
    if(!visited[it])
    dfs(it,adj,visited,c);
    }
    }
    int maximumDetonation(vector& bombs) {
    int n= bombs.size();
    vectoradj[n];
    for(int i=0;i

  • @horccruxxxop4184
    @horccruxxxop4184 Před rokem +1

    Time complexity?

    • @codestorywithMIK
      @codestorywithMIK  Před rokem +2

      We call BFS/DFS for each n node --- (i)
      For each node , we will have to visit n edges in worst case .
      Worst case, the number of edges can be (n-1)
      So it makes T.C = n*(n-1) ~ n^2 --- (ii)
      From equation (i) and (ii)
      TC ~ O(n^3)

  • @shubhtalk7073
    @shubhtalk7073 Před 2 měsíci

    Bahi questions ka input samjh nhi ayaa 😢😢

  • @everydaygkinhindi7583
    @everydaygkinhindi7583 Před rokem +2

    Sir can you also upload that PDF that you are making in video in GitHub?

    • @everydaygkinhindi7583
      @everydaygkinhindi7583 Před rokem +2

      There You written very nice it will be useful when we have to revise the concept.

    • @codestorywithMIK
      @codestorywithMIK  Před rokem

      Hi there,
      Actually i have not yet prepared the PDF yet.
      But i put all my code on Github, and have also mentioned the youtube link in the top of every solution.
      So that anyone sitting anywhere just open my github repo, open the problem in my github repo, see the code and also open the youtube link mentioned in the top of the code to see the video also

    • @everydaygkinhindi7583
      @everydaygkinhindi7583 Před rokem +1

      @@codestorywithMIK but at the time of revising we have to again watch the video.

    • @codestorywithMIK
      @codestorywithMIK  Před rokem

      Yes,
      I will later try to create PDF for sure ❤️❤️