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

Поділитися
Вставка
  • Опубліковано 20 гру 2024

КОМЕНТАРІ • 35

  • @PranayRadharapu
    @PranayRadharapu 11 місяців тому +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 11 місяців тому +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!!

  • @ARYANMITTAL
    @ARYANMITTAL  11 місяців тому +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 11 місяців тому

      Dp solution bhi dedo

    • @satwiktatikonda764
      @satwiktatikonda764 11 місяців тому +3

      first thing that comes up to my mind is dp

    • @shivammishra7671
      @shivammishra7671 11 місяців тому

      Bhaiya Dp solution also pls !!!

    • @shivammishra7671
      @shivammishra7671 11 місяців тому

      yup😅@@satwiktatikonda764

    • @lilgainz
      @lilgainz 11 місяців тому

      Bhai dp daaldena that was my first intuition

  • @bashirafarhin3435
    @bashirafarhin3435 7 місяців тому +2

    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

  • @rishikeshpawar3230
    @rishikeshpawar3230 11 місяців тому

    Very nice one..

  • @CodeRocks-z6g
    @CodeRocks-z6g 11 місяців тому

    Bro great work keep it up.

  • @SurajGupta-gc9tz
    @SurajGupta-gc9tz 11 місяців тому

    please continue in this manner only

  • @yashkhanna2459
    @yashkhanna2459 11 місяців тому

    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 11 місяців тому

      i also had a doubt why is second while loop used

    • @prasannakumar898
      @prasannakumar898 11 місяців тому

      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 11 місяців тому

      both works the same ig

  • @aryamanagarwal1977
    @aryamanagarwal1977 11 місяців тому

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

  • @AkashKumar-bm4py
    @AkashKumar-bm4py 10 місяців тому

    bro why not dp??

  • @prajwalshaw9217
    @prajwalshaw9217 11 місяців тому

    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 11 місяців тому +1

      I think it should be x + 11

  • @hackytech7494
    @hackytech7494 11 місяців тому

    Thank you

  • @ArpitGupta-n5l
    @ArpitGupta-n5l 11 місяців тому

    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;
    }
    };

  • @simplechips
    @simplechips 11 місяців тому

    What's the Complexity of Your solution?

  • @pavanemani3895
    @pavanemani3895 11 місяців тому

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

  • @akshanshsharma6025
    @akshanshsharma6025 11 місяців тому

    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

  • @satwiktatikonda764
    @satwiktatikonda764 11 місяців тому +1

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

    if x

  • @vaibhavchaubey8373
    @vaibhavchaubey8373 11 місяців тому +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

  • @sankhadip_roy
    @sankhadip_roy 5 місяців тому

    this guy is very much irritating, overact very much

  • @shivrajhulwan2235
    @shivrajhulwan2235 11 місяців тому

    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!