Help me out in this problem
c is indicating what step u have taken before right?
yeah bro ,pls wait. will ping u back once it get resolved
@shivammishra20121999
u solution looks correct.
one mistake that is there is u r not clearing ur map, so it will store answer of ur previous result.
also i believe this test case is wrong .
we cannot reach to 2000 from 0 using a->1999,b->2000 and forbidden state as [1998]
[1998]
1999
2000
2000
i know it is storing forbidden state.
let say
obj x=solution(); // made object of ur solutiuon class.
x.minjumps(passed on set of tes case) ; // it will give some entries in ur map right?
x.minjumps(passed another set of test case) ; // note becuase it is same object it will be holding ur previous forbidden state right?
thats why i was telling u to clear ur map
i have understood each bit of ur logic.
from i u have two options .
solve(i,c)-> min(1+solve(i+a,0) , 1+solve(i-b,c+1) );
base cases
if u reached to visited state or forbidden state u return big number
if u i<0 or i>4000 u return big numberm(indicates out of range)
if c>=2 that indicates u have take more than 1 backwar steps so u returned big number .
evrything is correct in ur logic.
one thing that might be creating an issue is ur search range
try increasinf ur range from 4000 to some big number.
it will work i guess
wait bro , give me sometime will try to resolve it asap
till then try another problem
pls check ur updated code here->
unordered_map<int,int>mp;
class Solution {
public:
int dp[8005][2];//increased size
int fun(int i, int c,int a, int b, int x)
{
// base condition
if(i<0 or i>8000 or c>=2 or mp.find(i)!=mp.end())return 1e9+1;// increased search domain
if(i==x)return 0;
if(dp[i][c]!=-1)
return dp[i][c];
dp[i][c]=1+fun(i+a,0,a,b,x);
dp[i][c]=min(dp[i][c],1+fun(i-b,c+1,a,b,x));
return dp[i][c];
}
int minimumJumps(vector<int>& forbidden, int a, int b, int x) {
mp.clear();//added
for(int i=0;i<forbidden.size();i++)
mp[forbidden[i]]=1;
memset(dp,-1,sizeof(dp));
int res=fun(0,0,a,b,x);
if(res>=1e9)return -1;
return res;
}
};
basically i just changed ur search space from 4000 , i changed it to 8000 (no logic behind this number just a random guess)
rest evrything was correct
now it is working bro.
pls try to submit