Shortest Bridge | Leetcode-934 | DFS + BFS | MICROSOFT | Explanation ➕ Live Coding

Поделиться
HTML-код
  • Опубликовано: 27 окт 2024

Комментарии • 60

  • @DivVj
    @DivVj Год назад +10

    Tussi great ho sirji 🙌🏻 ✨ Every day I wait for your video 😍

  • @codestorywithMIK
    @codestorywithMIK  Год назад +14

    Similar Problems and very good for practice.
    Leetcode 286. Walls and Gates
    Leetcode 417. Pacific Atlantic Water Flow
    Leetcode 994. Rotting Oranges
    Leetcode 1162. As Far from Land as Possible
    Leetcode 1765. Map of Highest Peak

    • @tauquirahmed1879
      @tauquirahmed1879 Год назад +1

      kkya matlab ye sb questions Graph Conepts waale playlist mein aane wala h 🤣😀

    • @codestorywithMIK
      @codestorywithMIK  Год назад

      Why not 🤓

    • @AnkitSingh-tm5dp
      @AnkitSingh-tm5dp Год назад +1

      Very thankful for giving the similar problem list leetcode graph medium ko aapne array asa simple kar dyia bas logic socho implement to koi bhi kar le.

    • @codestorywithMIK
      @codestorywithMIK  Год назад

      Thanks a lot Ankit ❤️❤️

    • @justanuhere
      @justanuhere 5 месяцев назад

      thank u so much bhaiya

  • @souravjoshi2293
    @souravjoshi2293 Год назад +8

    As always , L.E.G.E.N.D.
    From those story points, I stopped the video and started coding 🥳.
    No one can beat your teaching style.

    • @JJ-tp2dd
      @JJ-tp2dd Год назад

      Wow bhai, you are a rockstar too

  • @dhruvsagar6577
    @dhruvsagar6577 Год назад +3

    Similar problem mentioning in Comment section is very nice.
    Please continue this. Love and Peace from my side.

  • @tauquirahmed1879
    @tauquirahmed1879 Год назад +2

    I stopped watching the video at 7 minutes then I went and code on my own. I used a visited array to encounter the visited elements I got TLE. Then I watched the full video and used set. I never though that we could use set also to keep the visited array. Such a great explanation! ❣

    • @codestorywithMIK
      @codestorywithMIK  Год назад

      Wow. Thanks a lot
      I am so glad ❤️❤️❤️

    • @elakstein
      @elakstein Год назад

      Its possible to solve with visited array.Maybe you got TLE because of some infinite recursion.
      I solved with the following code :
      class Solution {
      private:
      int d[4][2] = {{0,1}, {0,-1}, {1,0}, {-1,0}};

      bool within_bound(int i, int j, int n){
      return (i>=0 && i=0 && j

  • @mdarifnufqqdugir3278
    @mdarifnufqqdugir3278 Год назад +1

    Bhaii kyaa intuition smjhate ho ek bar me dimag ke andar ..main aapke playlist se hi graph padh rha hau ❤❤❤

    • @codestorywithMIK
      @codestorywithMIK  Год назад +1

      Wow. You made my day.
      Thank you so much Arif ❤️❤️❤️

    • @mdarifnufqqdugir3278
      @mdarifnufqqdugir3278 Год назад +1

      Bhai kaha se hain aap main really kabhi na kabhi apse milna chahta hu..😛mjhe graph padhna tha par apke jaisa koi nhi padhta phr mjhe apka channel milaa to leetcode challenge shuru Kiya aur graphs bhii..love you bhaii may Allah bless you keep shining 🙏🙏

    • @codestorywithMIK
      @codestorywithMIK  Год назад +1

      Thanks a lot Arif.
      I am just a normal guy like you all.
      I am also learning and growing with you all

  • @snehagoyal4978
    @snehagoyal4978 Год назад +1

    codestorywithMIK is my goto channel for daily streak questions. I wait for you to upload the video instead of watching someone else's video as here we learn to build the intuition. Thank you Sir ❤

  • @MyCodingDiary
    @MyCodingDiary Год назад +2

    Your explanations are so clear and easy to understand. Thank you for making this video!🤝

  • @SahilGupta-cd8lc
    @SahilGupta-cd8lc Год назад +1

    best explanation on youtube

  • @anuppatankar4294
    @anuppatankar4294 Год назад +2

    Your teaching skills are on the next level 😍

  • @ashutoshpandey1639
    @ashutoshpandey1639 Год назад +4

    As always again you are amazing sir🔥, please please make video on leetcode contest upsolving i am eagerly waiting for this series because after giving contest i can't find any solid solutions for those problems which i was unable to solve , it will really helpful for every leetcoders whom giving contest seriously , it's a huge request sir🙏🙏

    • @codestorywithMIK
      @codestorywithMIK  Год назад +1

      Thanks a lot Ashutosh.
      I understand and will definitely plan to cover contests soon.
      Currently a little occupied because of DP concepts playlist
      But for sure soon

    • @ashutoshpandey1639
      @ashutoshpandey1639 Год назад +1

      @@codestorywithMIK thanks 🙏

  • @JJ-tp2dd
    @JJ-tp2dd Год назад +3

    Thank you KING of Graphs 👑👑 Java implementation:
    PS : Code might look lengthy because I have overridden equals and hashCode method to use Pair class in set. But this would work for any future code where you need this conversion as well.
    class Pair {
    int row;
    int col;

    Pair(int _row, int _col) {
    row = _row;
    col = _col;
    }

    @Override
    public int hashCode() {
    int hash = 3;
    hash = 47 * hash + this.row;
    hash = 47 * hash + this.col;
    return hash;
    }
    @Override
    public boolean equals(Object obj) {
    if (obj == null) {
    return false;
    }
    if (getClass() != obj.getClass()) {
    return false;
    }
    final Pair other = (Pair) obj;
    if (this.row != other.row) {
    return false;
    }
    if (this.col != other.col) {
    return false;
    }
    return true;
    }
    }
    class Solution {

    private int m;
    private int n;

    private int[][] directions = {
    {-1,0},
    {0,-1}, {0,1},
    {1,0}
    };

    private boolean isSafe(int i, int j) {
    if(i=m || j=n){
    return false;
    }
    return true;
    }

    private void dfs(int i, int j, int[][] grid, Set visitedCells) {

    if(!isSafe(i,j) || grid[i][j] == 0 || visitedCells.contains(new Pair(i,j))) {
    return;
    }

    visitedCells.add(new Pair(i,j));
    for(int[] dir : directions) {
    int i_ = i + dir[0];
    int j_ = j + dir[1];

    dfs(i_,j_,grid,visitedCells);
    }

    }

    private int bfs(int[][] grid, Set visitedCells) {

    Queue q = new LinkedList();
    for(Pair p : visitedCells) {
    q.offer(p);
    }

    int level = 0;
    while(!q.isEmpty()) {
    int L = q.size();
    while(L-- > 0) {
    Pair pair = q.poll();
    int x = pair.row;
    int y = pair.col;

    for(int[] dir : directions) {
    int x_ = x + dir[0];
    int y_ = y + dir[1];

    Pair p_ = new Pair(x_,y_);
    if(isSafe(x_,y_) && !visitedCells.contains(p_)) {
    if(grid[x_][y_] == 1) return level;
    visitedCells.add(p_);
    q.offer(p_);
    }
    }
    }

    level++;
    }
    return level;
    }

    public int shortestBridge(int[][] grid) {

    this.m = grid.length;
    this.n = grid[0].length;
    Set visitedCells = new HashSet();

    for(int i=0; i

  • @AlishaKhan-ww3io
    @AlishaKhan-ww3io Год назад

    One of the best explanations

  • @udaykumarchetla6205
    @udaykumarchetla6205 Год назад +2

    First I did a brute force approach. I stored all cells of first island by dfs. And then store the cells of second island and find the Manhattan distance for every combination of both islands. This got TLE. Then did multi source BFS until i got a 1 from second island.. got accepted

    • @ShubhamKumar-sj6dp
      @ShubhamKumar-sj6dp 9 месяцев назад +1

      But your approach will not get TLE , I have made submission exactly like your brute force

  • @AmandeepSingh-uq3wp
    @AmandeepSingh-uq3wp Год назад +1

    Good explanation 👌
    Could you please make a video of today's daily challenge like how to solve it with the help of bucket sort because solving it with the help of priority queue is simple and understandable but there are very less videos explaning how to use bucket sort for this question.

  • @snehagoyal4978
    @snehagoyal4978 Год назад +1

    excellent video

  • @abcd76820
    @abcd76820 6 месяцев назад

    Hi mik , i completed graph concept playlist . just a request can you make vdeo on how to find cut vertex and cut edge ie bridges in graph . It will helpful for sem exam too .

  • @souravjoshi2293
    @souravjoshi2293 Год назад +7

    WHY DON'T YOU EVER TELL US IN YOUR VIDEO TO SUBSCRIBE, LIKE or SHARE your contents.
    WHY ?????
    Janta jawab chahti hai

    • @ajit287
      @ajit287 Год назад +6

      Bro this is speciality of this channel. Subscribers yah par bol kar nahi aate balki khud chale aate hai.😀

    • @souravjoshi2293
      @souravjoshi2293 Год назад

      @@ajit287 True bro

  • @codingsamurais
    @codingsamurais Год назад +2

    Without using visited array it is even more fast means no need of visited array here

  • @vineetkumar2899
    @vineetkumar2899 Год назад +1

    Bhaiya thanks for this awesome explanation .Can you please suggest follow up for the same ?

    • @codestorywithMIK
      @codestorywithMIK  Год назад

      Thanks Vineet.
      If you click on my Github code link in the description, there are related problems for this Qn, you should definitely give then a try

  • @recessionriche
    @recessionriche Год назад +1

    Here's the Java code
    Tip: Java has a inbuilt Pair already, no need to make a class like others keep doing
    class Solution {
    int n, m;
    int[][] directions = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
    public boolean inBorder(int[][] grid, int i, int j) {
    return i >= 0 && j >= 0 && j < m && i < n;
    }
    public void dfs(int[][] grid, int i, int j, Set visited) {
    if (!inBorder(grid, i, j) || visited.contains(new Pair(i, j)) || grid[i][j] == 0) return;
    visited.add(new Pair(i, j));
    for (int[] dir : directions) {
    int _x = i + dir[0];
    int _y = j + dir[1];
    dfs(grid, _x, _y, visited);
    }
    }
    public int bfs(int[][] grid, int i, int j, Set visited) {
    Queue queue = new LinkedList();
    for (Pair p : visited) {
    queue.add(p);
    }
    int level = 0;
    while (!queue.isEmpty()) {
    int size = queue.size();
    while (size-- > 0) {
    Pair p = queue.poll();
    int first = p.getKey();
    int second = p.getValue();
    for (int[] dir : directions) {
    int _x = first + dir[0];
    int _y = second + dir[1];
    if (inBorder(grid, _x, _y) && !visited.contains(new Pair(_x, _y))) {
    if (grid[_x][_y] == 1) {
    return level;
    }
    visited.add(new Pair(_x, _y));
    queue.add(new Pair(_x, _y));
    }
    }
    }
    level++;
    }
    return level;
    }
    public int shortestBridge(int[][] grid) {
    n = grid.length;
    m = grid[0].length;
    Set visited = new HashSet();
    for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
    if (grid[i][j] == 1) {
    dfs(grid, i, j, visited);
    return bfs(grid, i, j, visited);
    }
    }
    }
    return -1;
    }
    }

  • @ugcwithaddi
    @ugcwithaddi Год назад

    Masterpiece

  • @codeandtalk6
    @codeandtalk6 Год назад +1

  • @rahulmaurya6451
    @rahulmaurya6451 Год назад +1

    Apka streak kaise reset ho gya ??

    • @codestorywithMIK
      @codestorywithMIK  Год назад

      I think , I had already made a video on Bipartite Graph long back. So i missed to solve it for streak this time.
      But dont worry, i will cover it with my coin 😁

  • @ManojKrVerma-vw4dx
    @ManojKrVerma-vw4dx Год назад +1

    I first got tle....