can anyone please explain what i am doing wrong in this code? been stuck here.
class Solution {
public:
int i=0;
int solve(int currPos,int target,int fuel,int count,map<int,int> &mp){
if(currPos == target)
{
cout<<count<<endl;
return count;
}
if(fuel==0 and currPos< target and mp.find(currPos)==mp.end()){
return INT_MAX;
}
int fill=INT_MAX,notFill=INT_MAX;
if(mp.find(currPos)!=mp.end())
fill=solve(currPos+1,target,fuel-1+mp[currPos],count+1,mp);
notFill = solve(currPos+1,target,fuel-1,count,mp);
// cout<<"currPos, fuel and count : "<<
// currPos<<" "<<fuel<<" "<<count<<endl;
// cout<<"fill and not fill : "<<fill<<" "<<notFill<<endl;
// cout<<endl;
return min(fill,notFill);
}
int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) {
map<int,int> mp;
for(auto it : stations){
mp[it[0]]=mp[it[1]];
}
int ans= solve(0,target,startFuel,0,mp);
if(ans==INT_MAX)
return -1;
return ans;
}
};
Problem Link : - LeetCode