Recursion + Memoization in Cpp..
\n# Code\n\nclass Solution {\npublic:\n\n    int func(int i,vector<int>&p,int k,int t,vector<vector<int>>&dp){\n\n        if(i>=p.size())return 0;\n        if(t
\n# Code\n```\nclass Solution {\npublic:\n\n    int func(int i,vector<int>&p,int k,int t,vector<vector<int>>&dp){\n\n        if(i>=p.size())return 0;\n        if(t<=0)return 0;\n\n        if(dp[i][t]!=-1)return dp[i][t];\n\n        int a=0,b=0;\n\n         int ind=upper_bound(p.begin(),p.end(),p[i]+k)-p.begin();\n\n        // cout<<ind<<" ";\n        b=ind-i+func(ind,p,k,t-1,dp);\n\n        a=func(i+1,p,k,t,dp);\n       \n\n        return dp[i][t] = max(a,b);\n    }\n\n    int maximizeWin(vector<int>& p, int k) {\n       \n       vector<vector<int>>dp(p.size(),vector<int>(3,-1));\n       return func(0,p,k,2,dp);\n    }\n};\n```
['C++']