Both 100% | C++ | 58 ms/54.6 MB
Intuition\n Describe your first thoughts on how to solve this problem. \nLet DP[i] represent the farthest distance that can be reached to the right at position 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nLet DP[i] represent the farthest distance that can be reached to the right at position i. The problem is to find x and y to maximize the sum of DP[x] + DP[y]. Note that the parts covered by x and y cannot overlap.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nUse two sliding windows, one is used to calculate dp[i] (because dp[i]+i is increasing to the right); the other is used to find max(dp[x]) for x<i (the reason is the same as above). Note when x=0, it means that all elements to the left of i are in the range k.\n\n# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ --> \n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int maximizeWin(vector<int>& p, int k) {\n        int n=p.size(),ans=0;\n        for(int i=0,r=0,l=0,v=0;i<n;++i){\n            while(r<n&&p[r]-p[i]<=k)++r;\n            p[i]=r-i;\n            while(p[l]+l<=i)v=max(v,p[l++]);\n            ans=max(ans,l?v+p[i]:p[i]+i);\n            if(r==n)return ans;\n        }\n        return ans;\n    }\n};\n```
['C++']