Skip to content

Commit

Permalink
Create MinimumMultiplicationToReachEnd.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitiz11101 authored Jun 28, 2024
1 parent 90863d4 commit 794f8a9
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Graphs/MinimumMultiplicationToReachEnd.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// User function Template for C++

class Solution {
public:
const int mod=100000;
int minimumMultiplications(vector<int>& arr, int start, int end) {
// code here
queue<pair<int,int>>q;
q.push({0,start});
vector<int>dist(100000,1e9);
dist[start]=0;
while(!q.empty()){
int steps=q.front().first;
int node=q.front().second;
q.pop();

for(auto iter:arr){
int num=(iter*node)%mod;
if(steps+1<dist[num]){
dist[num]=steps+1;
if(num==end){
return steps+1;
}
q.push({steps+1,num});
}
}

}
if(start==end){
return 0;
}
return -1;
}
};

0 comments on commit 794f8a9

Please sign in to comment.