Please Help Me Improve..!!!

Hello all,
I am preparing for my intervews ,practicing leetcode problems everyday. DP has been kind of a nightmare for me but I am getting used to it day by day.

Now, I came across this FROG JUMP problem and I was able to write recurscive solution . Ofcourse it gave TLE , but I was satisfied that I figured out the recursive solution.
Now to convert it into bottom up approach, I tried a 2D state like dp[size][size] //size is number od stones here.

I was able to convert after many failed attempts and my code succesfully got accepted with 5% better time than others , but that's not my point. My codes have always been messy, non-intutive , lengthy, time-taking etc. which is bad I know and I want to improve this bad habit.
Manytimes I code the solution to a problem and suppose my code has 50-60 odd lines and when I see some others solution for the same in dissucsions their code happens to be of say20-30 lines. I get very sad that why I am doing so many extra lines when I could be done in compact manner.
Please help me improve and I am open to criticism/suggestions.

For eg look at my code here : (for reference)

class Solution {
public:
unordered_map<long long int,int>mp;
// bool topDownDp(vector stones,int index,int siz,int lastHop,int k)
// {
// // cout<<index<<" "<<lastHop<<" "<<k<<"\n";
// if(k>siz)
// return false;
// if(lastHop<=0)
// return false;
// if(mp.find(index)==mp.end())
// return false;

// if(index==stones[siz])
// {
// // cout<<"mc";
// return true;
// }

// bool m= topDownDp(stones,index+lastHop-1,siz,lastHop-1,k+1) || topDownDp(stones,index+lastHop,siz,lastHop,k+1) || topDownDp(stones,index+lastHop+1,siz,lastHop+1,k+1);
// return m;
// }
bool canCross(vector& stones) {

   if(stones[1]-stones[0]>1)
       return false;
    
    for(int i=0;i<stones.size();i++)
    {
        mp.insert(make_pair(stones[i],i));
    }
    
    int dp[stones.size()+1][stones.size()+1];
    memset(dp,0,sizeof(dp));
    dp[0][0]=1;
    dp[0][1]=1;
    dp[1][0]=1;
    dp[2][1]=1;
    
    for(int i=2;i<stones.size();i++)
    {
        for(int j=1;j<=i;j++)
        {
            if(dp[i][j]==1)
            {
                long long int diff=abs(stones[i]-stones[j]);
               for(int k=1;k>=-1;k--)
               {
                   if(mp.find(stones[i]+diff-k)!=mp.end())
                   {
                       int index=mp[stones[i]+diff-k];
                       if(dp[i][index]==0)
                       {
                             dp[index][i]=1;
                            dp[i][index]=1;
                       }
                     
                   }
                  
               }
            }
            else 
            {
                 long long int diff=abs(stones[i]-stones[j]);
               for(int k=1;k>=-1;k--)
               {
                   if(stones[j]-diff+k>=0)
                   {
                       if(mp.find(stones[i]-diff+k)!=mp.end())
                       {
                           int index=mp[stones[i]-diff+k];
                           if(dp[i][index]==1)
                           {
                                dp[i][index]=1;
                               dp[index][i]=1;
                           }
                           
                       }
                   }
                   
                  
               }
            }
        }
    }
    
  
    for(int i=0;i<stones.size();i++)
    {
        if(dp[i][stones.size()-1]==1||dp[i][stones.size()-1]==-1)
            return true;
    }
    return false;
    
}

};
I know it could be done way more compact and easy than this.
If you have any valuable suggestion please I happy to take it.

Thank you!!!!

Comments (1)