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!!
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
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
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;
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.
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;
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❤
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!!
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
Dp solution bhi dedo
first thing that comes up to my mind is dp
Bhaiya Dp solution also pls !!!
yup😅@@satwiktatikonda764
Bhai dp daaldena that was my first intuition
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
Very nice one..
Bro great work keep it up.
please continue in this manner only
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
i also had a doubt why is second while loop used
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
both works the same ig
bhaiya can u plzz post the link for your race car submission
bro why not dp??
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.
I think it should be x + 11
Thank you
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;
}
};
What's the Complexity of Your solution?
class Solution:
def minimumOperationsToMakeEqual(self, x: int, y: int) -> int:
if x
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
python code using dp with memoization
dp={}
def dpbktrk(x):
if x
DP SOLUTION
/* Memoization */
// Time Complexity: O(x)
// Space Complexity: O(x)
class Solution {
public:
int helper(int x, int y, vector& dp){
if(x
this guy is very much irritating, overact very much
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!
good approch