Minimum Number of Operations to Make X and Y Equal | BFS | Graph | Similar to Race Car

Поделиться
HTML-код
  • Опубликовано: 20 авг 2024
  • In this video, I'll talk about how to solve Minimum Number of Operations to Make X and Y Equal
    Let's Connect:
    📝Linkedin: / aryan-mittal-0077
    📸 Instagram: / ez.pz.dsa
    📱Telegram : t.me/aryan_mit...
    🤖 Github: github.com/ary...
    About Me:
    I am Aryan Mittal - A Software Engineer in Goldman Sachs, Speaker, Creator & Educator. During my free time, I create programming education content on this channel & also how to use that to grow :)
    ✨ Timelines✨
    ✨ Hashtags ✨
    #programming #Interviews #leetcode #faang #maang #datastructures #algorithms

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

  • @PranayRadharapu
    @PranayRadharapu 7 месяцев назад +5

    Greatly explained!!
    I wasn't able to solve this in the contest. but i solved the 4th one because of your digit dp explanation. Thanks brother❤

  • @himanshuchand7272
    @himanshuchand7272 7 месяцев назад +4

    bro started explaining BFS even before explaining the idea to choose bfs for this question!!. This question can be done using DP too but why to choose bfs over dp here?? You need to explain the thought process of selecting a algorithm before explaining direct solution. This can be done easily by seeing any problem discussion section!!

  • @bashirafarhin3435
    @bashirafarhin3435 3 месяца назад +1

    ps: no need to main distance map....just write another if condition
    if (node == y) { return dist; }
    Another point ------>
    in 4th if condition write node+1

  • @ARYANMITTAL
    @ARYANMITTAL  7 месяцев назад +10

    This problem, along with Race Car problem, both can be solved with DP too, but still i feel BFS is much more intuitional than that of DP, what are your views?? - Though i feel its not that medium TBH

    • @_Aryan_Chandel
      @_Aryan_Chandel 7 месяцев назад

      Dp solution bhi dedo

    • @satwiktatikonda764
      @satwiktatikonda764 7 месяцев назад +3

      first thing that comes up to my mind is dp

    • @shivammishra7671
      @shivammishra7671 7 месяцев назад

      Bhaiya Dp solution also pls !!!

    • @shivammishra7671
      @shivammishra7671 7 месяцев назад

      yup😅@@satwiktatikonda764

    • @lilgainz
      @lilgainz 7 месяцев назад

      Bhai dp daaldena that was my first intuition

  • @rishikeshpawar3230
    @rishikeshpawar3230 7 месяцев назад

    Very nice one..

  • @SurajGupta-gc9tz
    @SurajGupta-gc9tz 7 месяцев назад

    please continue in this manner only

  • @user-gu8hk3ub9f
    @user-gu8hk3ub9f 7 месяцев назад

    Bro great work keep it up.

  • @hackytech7494
    @hackytech7494 7 месяцев назад

    Thank you

  • @AkashKumar-bm4py
    @AkashKumar-bm4py 6 месяцев назад

    bro why not dp??

  • @user-cq1wi1ip9n
    @user-cq1wi1ip9n 7 месяцев назад

    class Solution {
    public:
    int minimumOperationsToMakeEqual(int x, int y) {
    if(x = MAX)
    continue;
    if(num % 11 == 0 && !vis[num / 11]) {
    vis[num / 11] = true;
    q.push({ num / 11, steps + 1 });
    }
    if(num % 5 == 0 && !vis[num / 5]) {
    vis[num / 5] = true;
    q.push({ num / 5, steps + 1 });
    }
    if(!vis[num - 1]) {
    vis[num - 1] = true;
    q.push({ num - 1, steps + 1 });
    }
    if(!vis[num + 1]) {
    vis[num + 1] = true;
    q.push({ num + 1, steps + 1 });
    }
    }
    return INT_MAX;
    }
    };

  • @aryamanagarwal1977
    @aryamanagarwal1977 7 месяцев назад

    bhaiya can u plzz post the link for your race car submission

  • @ritikachoudhary7181
    @ritikachoudhary7181 7 месяцев назад

    What's the Complexity of Your solution?

  • @prajwalshaw9217
    @prajwalshaw9217 7 месяцев назад

    Just one question..while doing the fourth operation..i.e. increasing x by +1...why are we check that it is less than x+15...?..didnt understand this line of code. Anyways understood the entire approach. Thanks for a wonderful explanation.

    • @TON-108
      @TON-108 7 месяцев назад +1

      I think it should be x + 11

  • @pavanemani3895
    @pavanemani3895 7 месяцев назад

    class Solution:
    def minimumOperationsToMakeEqual(self, x: int, y: int) -> int:
    if x

  • @yashkhanna2459
    @yashkhanna2459 7 месяцев назад

    is there a need for the inner while loop in this question as you are updating the distance when you push the node into the queue

    • @prasannakumar898
      @prasannakumar898 7 месяцев назад

      i also had a doubt why is second while loop used

    • @prasannakumar898
      @prasannakumar898 7 месяцев назад

      but it works without nested while loop
      code
      class Solution {
      public:
      int minimumOperationsToMakeEqual(int x, int y)
      {
      if(y>=x)return y-x;
      vectorvisited(x+15,0);
      queueq;
      q.push({x,0});
      visited[x]=1;

      while(!q.empty())
      {
      pairp=q.front();
      int node=p.first;
      int dis=p.second;
      q.pop();
      if(node==y)return dis;

      if(node%11==0 && !visited[node/11])
      {
      visited[node/11]=1;
      q.push({node/11,dis+1});
      }
      if(node%5==0 && !visited[node/5])
      {
      visited[node/5]=1;
      q.push({node/5,dis+1});
      }
      if(node-1>=0 && !visited[node-1])
      {
      visited[node-1]=1;
      q.push({node-1,dis+1});
      }
      if(node+1

    • @jagadeeshp1163
      @jagadeeshp1163 7 месяцев назад

      both works the same ig

  • @akshanshsharma6025
    @akshanshsharma6025 7 месяцев назад

    this is just awesome explanation i wrote the code by myself thank you so much bro
    int minimumOperationsToMakeEqual(int x, int y)
    {
    if(y>=x)return y-x;
    vectorvisited(x+15,0);
    queueq;
    q.push({x,0});
    visited[x]=1;

    while(!q.empty())
    {
    pairp=q.front();
    int node=p.first;
    int dis=p.second;
    q.pop();
    if(node==y)return dis;

    if(node%11==0 && !visited[node/11])
    {
    visited[node/11]=1;
    q.push({node/11,dis+1});
    }
    if(node%5==0 && !visited[node/5])
    {
    visited[node/5]=1;
    q.push({node/5,dis+1});
    }
    if(node-1>=0 && !visited[node-1])
    {
    visited[node-1]=1;
    q.push({node-1,dis+1});
    }
    if(node+1

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

    DP SOLUTION
    /* Memoization */
    // Time Complexity: O(x)
    // Space Complexity: O(x)
    class Solution {
    public:
    int helper(int x, int y, vector& dp){
    if(x

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

    python code using dp with memoization
    dp={}
    def dpbktrk(x):

    if x

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

    this guy is very much irritating, overact very much

  • @shivrajhulwan2235
    @shivrajhulwan2235 7 месяцев назад

    int minimumOperationsToMakeEqual(int x, int y) {
    queue q;
    set vis;
    q.push(x);
    vis.insert(x);

    int cnt=0;
    while(!q.empty()){
    int n = q.size();

    for(int i=0; i0 && vis.find(ele-1) == vis.end()){
    vis.insert(ele-1);
    q.push(ele-1);
    }

    if(vis.find(ele+1) == vis.end()){
    vis.insert(ele+1);
    q.push(ele+1);
    }
    }

    cnt++;
    }

    return -1;
    }
    Thanks Aryan!