🐱 Well-Commented Solution: Brute Force DP to Efficient DP
Approach1
Convert to LIS: Reverse the array or iterate from the end to simplify the problem to finding the Longest Increasing Subsequence (LIS).
Track increasin
# Approach1
1.	Convert to LIS: Reverse the array or iterate from the end to simplify the problem to finding the Longest Increasing Subsequence (LIS).
2.	Track increasing diffs: For each num[i], compute the absolute difference with previous elements and update the longest subsequence based on valid differences.
3.	Use dynamic ends array: Store pairs of (diff, length) in ends[i], where diff is the absolute difference and length is the subsequence length. Update the subsequence length when a valid difference is found.
 4. Stack with TL

# Complexity
- Time complexity:
O(n^3), n - len of nums

- Space complexity:
O(n^2), n - len of nums

# Code
```javascript []
var longestSubsequence = function(nums) {
    nums.reverse();
    
    let totalMax = 0;

    const ends = Array.from({length: nums.length}, () => []);

    for(let i = 1; i < nums.length; i++){
        for(let j = i - 1; j >= 0; j--){
            const diff1 = Math.abs(nums[i] - nums[j]);
            let maxLen = 0;
            
            for(let k = 0; k < ends[j].length; k++){
                const [diff, len] = ends[j][k];
                
                //or remove nums.reverse and find LDS (diff1 <= diff)
                if(diff1 >= diff){
                    maxLen = Math.max(maxLen, len);
                }
            }
        
            ends[i].push([diff1, maxLen + 1]);
            totalMax = Math.max(totalMax, maxLen + 1);
        }
    }

    return totalMax + 1;
};
```

Result: TL

# Approach2
1. 	Use dynamic programming:
dp[i][j] - len of LIS where
	•	nums[i] is the last number
	•	j is the last absolute difference
2. Traverse nums from right to left to simplify the logic for increasing subsequences

# Complexity
- Time complexity:
O(n * maxDiff), n - len of nums

- Space complexity:
O(maxNum * maxDiff)

# Code
```javascript []
var longestSubsequence = function (nums) {
    const n = nums.length;
    const maxNum = Math.max(...nums);
    const maxDiff = maxNum - Math.min(...nums);

    const dp = Array.from({length: maxNum + 1}, () => Array(maxDiff + 1).fill(0));
    let totalSubseqs = 1;

    // traverse nums from right to left (convert task to 'find longest increasing subsequence of diffs')
    for (let i = n - 1; i >= 0; i--) {
        const num = nums[i];

        //control increasing order
        for (let diff = 0; diff <= maxDiff; diff++) { 
            const currentLen = dp[num][diff];
            const prevLen = dp[num][diff - 1] || 0;
            dp[num][diff] = Math.max(
                    currentLen, prevLen,
                    // for each num, find the previous values that can form the diff: [num - diff, num] or [num + diff, num]
                    1 + (dp[num - diff]?.[diff] || 0),
                    1 + (dp[num + diff]?.[diff] || 0)
            );

            totalSubseqs = Math.max(totalSubseqs, dp[nums[i]][diff]);
        }
    }

    return totalSubseqs;
};
```

PS Please let me know if you see any improvements or if this solution might fail in specific cases.
['Dynamic Programming', 'JavaScript']