G-14. Surrounded Regions | Replace O's with X's | C++ | Java

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

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

  • @takeUforward
    @takeUforward  2 года назад +104

    Let's continue the habit of commenting “understood” if you got the entire video. Please give it a like too, you don't 😞
    Do follow me on Instagram: striver_79

    • @sukhpreetsingh5200
      @sukhpreetsingh5200 2 года назад +1

      I am able to solve this question myself!! thanks a lot all this is because of you....

    • @jatilyadav4000
      @jatilyadav4000 2 года назад

      understood.... Best explaination

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

      understood

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

      bhiya why cant we have no visited matrix in this soln,we can use the matrix given to us and to record the visited we can change it to '1' and while backtracking we can undo the changes

    • @challasusmithachallasusmit9921
      @challasusmithachallasusmit9921 2 месяца назад

      Understood Everything Thank you so Much

  • @shivyanshgarg2641
    @shivyanshgarg2641 11 месяцев назад +32

    I am so happy to share that from lec 9 10 12 13 14 . I am able to solve them by myself without watching the whole video. After my solution gets accepted . I come and watch your video at 2x

  • @anushkajain1843
    @anushkajain1843 Год назад +62

    The logic was really great.
    We instead of finding who will be marked cross , we found out who will not through the boundary connection condition .
    Kudo striver for making this series.

  • @ShaliniNukella
    @ShaliniNukella 9 месяцев назад +4

    You've worked really hard to make these videos, mainly without getting irritated, you've coded each and every part. I realised how much time and PATIENCE is required to do that.... Thanks a lot

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

      Yeah, this is the best thing about Striver's videos!! He is EXTREMELY PATIENT while explaining any logic and coding it up till the end!! Just follow his recursion series, you will get to know how much patient he can be...

  • @HarshRaj-kk7rd
    @HarshRaj-kk7rd Год назад +12

    the energy you bring while teaching is just amazing

  • @sahilbani7020
    @sahilbani7020 6 месяцев назад +4

    love how after his all set of examples and intuitions, everything just falls in place perfectly

  • @tasneemayham974
    @tasneemayham974 9 месяцев назад +1

    Honestly, Striver!! Amazing LOGIC! If we think of it like you did it becomes a walk in the park!!
    UNDERSTOODD!! THANK YOUUU!!

  • @abdalla4425
    @abdalla4425 10 месяцев назад +2

    I know it had something to do with border and how they connect. First attempt was to try to find which ones reach the border but then I realized it would be easier to find what ones the border reaches. From their I had it down, solved it, and watched the video as a refresher/check for tips or tricks.

  • @sonugupta147
    @sonugupta147 9 месяцев назад +3

    My initial thoughts: start the ans matrix of size m*n with 'X's. If we have any 'O's on the boundary, treat them as a door or entrance and apply DFS from that cell. The DFS will make sure that it covers every O's cells which are connected to this entrance. Once DFS is done it would have marked all the affected cells as O's and rest are just 'X's. So in this way all the O's cells surrounded by boundary of X's will be marked as X.

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

    i just saw your explanation of what the problem really is in the first few minutes, and then coded it using dfs and passes all the test cases on leetcode, thankyou so much striver , its due to your teaching that i am elevating my coding skills each day

  • @009_akshayrajput
    @009_akshayrajput Год назад +1

    "Understood", Marvelous explanation with high energy !!

  • @albatrossgeez6637
    @albatrossgeez6637 11 месяцев назад +1

    Striver bro!!! I just want to hug you.............lots of respect and love and keep doing what you are doing...

  • @saurabhtiwari287
    @saurabhtiwari287 2 года назад +4

    Maza aa gya bhayiya ☺☺☺
    Lg hi nhi rha tha ki hard problem kr rhe h. Aaisa level set kr dya aapne hm sbka

  • @NaveensinglaYT
    @NaveensinglaYT 2 года назад +14

    such a good and refined series absolutely loved it striver bhai

  • @Anoyashu
    @Anoyashu Год назад +5

    till now in graph series, its all about how well we have understood DFS or BFS , rest is variation !!!

  • @Benstokes555
    @Benstokes555 7 месяцев назад +1

    YOURE MY DIAMOND STRIVES :)

  • @arunachalamm3399
    @arunachalamm3399 4 месяца назад

    saw 3 min, found intuition thanks to striver bhai and coded it, got AC, came and watched entire video to contribute to takeUForward's watch time and recommendation

  • @vipulchaudhary_js
    @vipulchaudhary_js 2 года назад +6

    I was trying to solve this question on leetcode this evening and Guess what You uploaded a solution!

  • @arthurdark3945
    @arthurdark3945 2 года назад +14

    Awesome series! Can you cover graph questions that were asked in OAs in your experience or someone else's so that we can learn what type of questions are actually asked?

    • @chandannikhil8526
      @chandannikhil8526 2 года назад +11

      Bhai gfg ke jobhathon dekho almost every contest mai hard ques graph based hote hai use bhaiya samjhate hai contest review mai

    • @arthurdark3945
      @arthurdark3945 2 года назад +1

      @@chandannikhil8526 Thank you will check those videos out

  • @introvert3487
    @introvert3487 2 года назад +5

    Barhe log aa gye ...love you warsaw bhaiya ❤️

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

    I understood your explanation, paused your video and tried it with BFS, it worked. Amazing explanation

  • @ASHISHKUMAR-o4c6x
    @ASHISHKUMAR-o4c6x Год назад +1

    because of you i am easily able to understand graph
    thank you sir......

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

    Able to solve the problem just by seeing till 2:40 of the video...power of Strivers 😎

  • @juniorboy1903
    @juniorboy1903 2 года назад +1

    Aap question aur code dono ko bahut easy bana dete ho bhaiya😀😍

  • @1tav0
    @1tav0 2 года назад +6

    Awesome, thank you striver as always understood

  • @roydebraj2188
    @roydebraj2188 4 месяца назад

    striver thanks alot fot your effort in making such an amazing in depth free course , i just watched you first 6 - 7 videos and i am telling you i am able to solve medium level questions on leetcode it really boost up up confidence thanks alot striver

  • @monty6393
    @monty6393 2 года назад +4

    Oho man just did number of island Question on leetcode :) it was a daily challenge problem exactly the same type of Question like this one :) ..did with recursion went up ,down left, right and follow up the constraints :)

  • @VivekVerma-oo3dx
    @VivekVerma-oo3dx Год назад

    able to do this question without watching the solution it really boosted my confidence thaku striver

  • @rohan8758
    @rohan8758 6 месяцев назад +1

    Great explanations, I was wondering here for complex problem solution I am getting training as doctor strange got from guru maa in kathmandu!

  • @stith_pragya
    @stith_pragya 11 месяцев назад +1

    Thank You So Much for this wonderful video............🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @shreyasinha4016
    @shreyasinha4016 Месяц назад

    solved on my own without watching the video on basis of prev videos and now watching

  • @varunaggarwal7126
    @varunaggarwal7126 Год назад +45

    I was so frustrated, I write '0' instead of 'O' and wondering why it is not working for 1 hour 🤦‍♂🤦‍♂

    • @anonymousanonymous7507
      @anonymousanonymous7507 11 месяцев назад +1

      Good

    • @artofwrick
      @artofwrick 9 месяцев назад +1

      I made a mistake in edge case, and spent 2 hours figuring where it went wrong. It took me the first hour just to merely accept that there was an error in the edge case

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

      same dude

    • @KapilSharma56419
      @KapilSharma56419 4 месяца назад

      compilation error nhi kiya 0 int hai O char hai

    • @ooofrg4492
      @ooofrg4492 3 месяца назад

      Shabash❤😂

  • @goatwice
    @goatwice 4 месяца назад +1

    using jS one can make a game that contain the player which is surrounded by a protection layer and enemy can enter through the boundry points

  • @manojnavinjamuri5867
    @manojnavinjamuri5867 2 года назад +3

    thank you striver for creating such amazing content

  • @HEMANTPORWAL-t7d
    @HEMANTPORWAL-t7d 6 месяцев назад

    Nice explaination sir but now gfg has increased test cases to 1,120 and with this approach only 1,116 test cases are getting passed
    thank u for your efforts

  • @developer00007
    @developer00007 6 месяцев назад +1

    Understood the solution in first 6 mins.
    Paused the video and solved the question in 10 mins 😉

  • @santanu29
    @santanu29 Год назад +5

    19:30 I think rather than creating delRow and delCol arrays you can just mention the dfs function call 4 times. I will save a lot on the parameters per call.
    Code:
    void dfs(int row, int col, vector& mat, vector& vis)
    {
    if(row

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

      Time limit exceeds : 1032/1044 passed

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

      @@arshdeep011 Are you the cricketer Arshdeep Singh?

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

      @@rushidesai2836 I don't watch cricket

  • @DevashishJose
    @DevashishJose 10 месяцев назад

    Understood, Thank you so much, this was such a beautiful solution.

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

    excellent explanation!

  • @syedmohammadhussain7137
    @syedmohammadhussain7137 2 года назад +1

    thanks man,same question came in my cohesity interview 40lpa

  • @sakshisahu3983
    @sakshisahu3983 2 года назад +1

    Thank you striver for all your efforts.

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

    Solved this one by self on BFS and DFS Both.

  • @chandannikhil8526
    @chandannikhil8526 2 года назад +2

    Understood bhaiya i have done bfs traversal

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

    This question can also be done like previous one using bfs we just. Need to put zero which are at the boundary in the queue then we have to run while loop until queue becomes empty and parallely we need to mark visited to the neighbouring zeros of boundary zeros....then we have to convert zero to x which are not visited 😅

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

    // To traverse the boundary
    for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
    if ((i == 0 || i == n - 1) || (i > 0 and i < n - 1 and (j == 0 || j == m - 1))) {
    // ToDo
    }
    }
    }

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

    kaya bat hai baiya ,maza aagya apka obserwation se

  • @viholvishwassinh1709
    @viholvishwassinh1709 Месяц назад +1

    khatarnak insted of finding who will be mark as 'X' we will find whol will not such a great idea

  • @harleenkaur7751
    @harleenkaur7751 2 года назад +1

    Thanks a lotttt striver for such an amazing playlist

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

    Understood!! Very well explained!!!

  • @harshvardhansingh2272
    @harshvardhansingh2272 2 года назад +1

    understood striver
    Awesome explanation as always

  • @rrp3509
    @rrp3509 2 года назад +3

    Superb as usual. But the first time I found energy and excitement low during explanation.
    Keep your uniqueness full of energy and excitement.❤️

    • @takeUforward
      @takeUforward  2 года назад +11

      Repeated questions, new questions excite me ;)

    • @RohitSharma-yc8lm
      @RohitSharma-yc8lm 2 года назад

      @@takeUforward 💛 thank you so much striver for these playlists , i learned recursion tree dp nd now graphs too.

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

    Great explanation 👍

  • @sripriyapotnuru5839
    @sripriyapotnuru5839 2 года назад

    Thank you, Striver 🙂

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

    day by day getting confidence while doing graph question able to solve this question myself #thanks Wizard Striver

  • @RajeshKumar._.16
    @RajeshKumar._.16 Год назад

    very nice approach and explanation

  • @nishantsah6981
    @nishantsah6981 2 года назад +13

    Will it be a good practice if in dfs traversal I change the boundary connected component to some random character (suppose 'Y') and then traverse the matrix where ever I find a 'O' I convert it to 'X' and change those 'Y' back to 'O'....ultimately this will change only the required 'O' ->'X'

    • @takeUforward
      @takeUforward  2 года назад +8

      you can! the main aim of the video is to teach that dfs or bfs can be used to solve the problem, can have various approaches.

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

      i did the same

  • @dharsan.s7937
    @dharsan.s7937 2 года назад +1

    Legend is back

  • @susanthomas223
    @susanthomas223 9 месяцев назад

    Thank you so much for such a detailed video

  • @cinime
    @cinime 2 года назад +1

    Understood! Is this how to fill by one color in an area surrounded by different color? Anyway, so amazing explanation as always, thank you very much!!

  • @RohitKumar-dz8dh
    @RohitKumar-dz8dh Год назад +1

    Understood 😊

  • @kshithi2864
    @kshithi2864 2 года назад

    Understood😁Thanks a lot Striver😃

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

    just watched 1 min and solved the whole problem damn

  • @World-Of-Mr-Motivater
    @World-Of-Mr-Motivater Год назад +1

    striver could we add the delrow[] and delcol[] inside the function itself ?rather than passing it as a parameter

  • @kaichang8186
    @kaichang8186 2 месяца назад

    understood, thanks for the effort on the great video

  • @sreyashisaha6852
    @sreyashisaha6852 3 месяца назад

    In these type of graph problems, if it is not mentioned whether the neighbors are top, down, left and right; or all the 9 neighbours, then which one do we consider by default?

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

    understood very well!!!

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

    Thank you very much. You are a genius.

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

    Thank You striver Bhaiya 🤟

  • @rishabhagarwal8049
    @rishabhagarwal8049 2 года назад

    Understood Sir, Thank you very much

  • @albatrossgeez6637
    @albatrossgeez6637 11 месяцев назад

    You are my inspiration ......................hats offfffffffffffffffffffff

  • @tejakumarreddyc2357
    @tejakumarreddyc2357 2 года назад +2

    in order to traverse in the first row, column and last row, column
    we can use this ( rows & columns will have index either 0 or m-1 or n-1)
    for(int i =0; i < n ; i++)
    {
    for(int j = 0 ; j < m; j++)
    {
    if((i == 0 || j == 0 || i == n-1 || j == m-1)&&
    mat[i][j] == 'O' && vis[i][j]==0)
    {
    dfs(i,j, mat , vis,result);
    }
    }
    }

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

      It has m*n complexity, our solution has m+n complexity

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

    Majaa Aagaya😜

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

    UNDERSTOOD!!!!!!!!

  • @shriyanshjain4444
    @shriyanshjain4444 Год назад +5

    Solved it without starting the video
    Thank you Raj bhaiya for this wonderful playlist!
    CODE:
    class Solution {
    private:
    int dir[5] = {0,-1,0,1,0};
    void bfs(int r, int c, vector &board, vector &safe){
    safe[r][c] = true;
    queue q;
    q.push({r,c});
    while(!q.empty()){
    int row = q.front().first;
    int col= q.front().second;
    q.pop();
    for(int i = 0; i < 4; ++i){
    int nrow = row + dir[i], ncol = col + dir[i+1];
    if(nrow >= 0 && ncol >= 0 && nrow < board.size() && ncol < board[0].size() && board[nrow][ncol] != 'X' && !safe[nrow][ncol]){
    safe[nrow][ncol] = true;
    q.push({nrow,ncol});
    }
    }
    }
    }
    public:
    void solve(vector& board) {
    int m = board.size(), n = board[0].size();
    vector safe(m,vector(n,false));
    for(int i = 0; i < m; ++i){
    for(int j= 0; j < n; ++j){
    if(i == 0 || j == 0 || i == m-1 || j == n-1){
    if(board[i][j] != 'X' && !safe[i][j]){
    bfs(i,j,board,safe);
    }
    }
    }
    }
    for(int i =0; i < m ; ++i){
    for(int j = 0; j < n; ++j){
    if(board[i][j] != 'X' && safe[i][j] == true){
    continue;
    }else{
    board[i][j] = 'X';
    }
    }
    }
    }
    };

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

      Hello, I also tried to solve without watching this video. Can you help me with this code, why it got stuck at 41/58 TC on leetcode :-
      class Solution {
      public:
      void solve(vector& board)
      {
      int n = board.size();
      int m = board[0].size();
      vector visited(n, vector (m,0));
      vector temp(n, vector (m,'X'));
      queue q;
      for(int i = 0; i

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

      @@flsh1343 class Solution {
      public:
      void solve(vector& board) {
      int n = board.size();
      int m = board[0].size();
      vector visited(n, vector (m,0));
      vector temp(n, vector (m,'X'));
      queue q;
      for(int i = 0; i

  • @rudranshagg2580
    @rudranshagg2580 8 месяцев назад

    BFS version of the logic (With comments 🚀🌟):
    class Solution {
    public:
    void solve(vector& board) {
    // I did using BFS. Can do it with DFS as well.

    // if an 'O' will be on the edge of the board we will not flip it
    // Start from the O's which are present on the boundary and find all the
    // O's that are connected to the boundary O's , these all O's will not
    // be converted , and all the other O's will be converted
    // have a visited vector ..yes to avoid going back to the parent indexes
    // (i,j)??
    queue q;
    int r = board.size();
    int c = board[0].size();
    vector vis(r, vector(c, 0));
    for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
    if (i == 0 || j == 0 || i == r - 1 || j == c - 1) {
    if (board[i][j] == 'O') {
    // shows this can't be fliped
    board[i][j] = '9';
    cout

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

    understood, thanks for such playlist!!

  • @udaypratapsingh8923
    @udaypratapsingh8923 2 года назад

    here we go !

  • @RohitSharma-yc8lm
    @RohitSharma-yc8lm 2 года назад +1

    #UNDERSTOOD

  • @_sf_editz1870
    @_sf_editz1870 3 месяца назад

    A little readable java code may be : )) class Solution {
    public void solve(char[][] board) {
    //here the boundary zeros cant be converted so we just mark as x and their connectivity
    //after the connectivities are done from boundaries
    //we need to check like if its a 'O' and not visited from boundary then it definetly is surrounded by x's so we can make it as x
    int visited[][] = new int[board.length][board[0].length];
    //first row and first col checking for '0s'
    for(int i=0; i

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

    solved without even looking solution that's the power of striver

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

    Understood Sir!

  • @maheshdhage5328
    @maheshdhage5328 2 года назад

    always love ur vidos and u brother😍

  • @project_eth
    @project_eth 4 месяца назад

    *understood*, day 1 of documenting myself

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

    God of dsa🙏

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

    UNDERSTOOD.

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

    Understood Bhaiya

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

    ek dum mast

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

    Thanks you very much bro for the series

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

    Thank you sir

  • @mihirsaini592
    @mihirsaini592 2 года назад

    Great intuition

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

    No visited matrix required.
    No extra space required.
    I did it without watching video in first attempt.
    Here's the code for learning:
    void dfs (int i, int j, int n, int m, vector& mat) {
    if (i < 0 || i == n || j < 0 || j == m || mat[i][j] == 'X' || mat[i][j] == '1') return;
    mat[i][j] = '1';
    dfs (i+1, j, n, m, mat);
    dfs (i, j+1, n, m, mat);
    dfs (i-1, j, n, m, mat);
    dfs (i, j-1, n, m, mat);
    }
    vector fill(int n, int m, vector mat)
    {
    // code here
    for (int i{0}; i < n; i++) {
    for (int j{0}; j < m; j++) {
    if ((i == 0 || i == n-1 || j == 0 || j == m-1) && mat[i][j] == 'O') {
    dfs (i, j, n, m, mat);
    }
    }
    }
    for (int i{0}; i < n; i++) {
    for (int j{0}; j < m; j++) {
    if (mat[i][j] == 'O') {
    mat[i][j] = 'X';
    } else if (mat[i][j] == '1') {
    mat[i][j] = 'O';
    }
    }
    }
    return mat;
    }

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

    understood bhaiya

  • @aryashjain7893
    @aryashjain7893 2 года назад

    understood , thnk u striver

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

    Just Wow observation

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

    Understood thank you sir

  • @Learnprogramming-q7f
    @Learnprogramming-q7f 7 месяцев назад

    Thank you Bhaiya

  • @zaiem6981
    @zaiem6981 Месяц назад

    Simpler version.
    from the initial loop, perform DFS only on 'O's and the boundary i.e 0, n -1, m-1. Mark the adjacent ''O's.
    class Solution {
    void dfs(char[][] board, int i, int j) {
    int n = board.length;
    int m = board[0].length;
    // Boundary check or if the cell is not 'O', stop DFS
    if (i < 0 || i >= n || j < 0 || j >= m || board[i][j] != 'O') {
    return;
    }
    // Mark this cell as part of the boundary region
    board[i][j] = 'B';
    // Visit the 4 connected neighbors
    dfs(board, i + 1, j); // down
    dfs(board, i - 1, j); // up
    dfs(board, i, j + 1); // right
    dfs(board, i, j - 1); // left
    }
    public void solve(char[][] board) {
    int n = board.length;
    if (n == 0)
    return;
    int m = board[0].length;
    // Step 1: Perform DFS from all 'O's on the boundary
    for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
    // Check the edges
    if ((i == 0 || i == n - 1 || j == 0 || j == m - 1) && board[i][j] == 'O') {
    dfs(board, i, j); // Start DFS for boundary 'O's
    }
    }
    }
    // Step 2: Flip the inner 'O's to 'X' and revert the boundary 'B's back to 'O'
    for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
    if (board[i][j] == 'O') {
    board[i][j] = 'X'; // Enclosed 'O' becomes 'X'
    } else if (board[i][j] == 'B') {
    board[i][j] = 'O'; // Boundary 'B' becomes 'O' again
    }
    }
    }
    }
    }

  • @gangsta_coder_12
    @gangsta_coder_12 2 года назад +1

    Understood 😀😀

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

    Thank you so much for this. it was helful

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

    understood paaji