Solve fastest DP store longest sequence with difference atmost difference in C++
Code
# Code
```cpp []
class Solution {
public:
    int longestSubsequence(vector<int>& nums) {
        vector<int> vec = nums;
        int n = nums.size(),finalRes = 1;
        
        initializeBaseCases(n);
        initializeMxArray();
        
        for(int i = 1; i < n; i++) {
            for(int j = 0; j < i; j++) {
                int d = abs(nums[i] - nums[j]);
                dp[i][d] = max(dp[i][d], mx[j][d] + 1);
                finalRes = max(finalRes, dp[i][d]);
            }
            updateMxArray(i);
        }
        return finalRes;
    }
    
private:
    static int dp[10001][301], mx[10001][301];  
    
    void initializeBaseCases(int n) {
        for(int i = 0; i < n; i++) {
            for(int d = 0; d < 300; d++) {
                dp[i][d] = 1;
                mx[i][d] = 1;
            }
        }
    }
    
    void initializeMxArray() {
        for(int d = 300; d >= 0; d--) {
            mx[0][d] = 1;
        }
    }

    void updateMxArray(int i) {
        mx[i][300] = dp[i][300];
        for(int d = 299; d >= 0; d--) {
            mx[i][d] = max(mx[i][d + 1], dp[i][d]);
        }
    }
};

int Solution::dp[10001][301];
int Solution::mx[10001][301];

```
['C++']