Talentd
FleetCode

3409. Longest Subsequence With Decreasing Adjacent Difference

Medium17.3% acceptance2 companies

You are given an array of integers nums.

Your task is to find the length of the longest subsequence seq of nums, such that the absolute differences between consecutive elements form a non-increasing sequence of integers. In other words, for a subsequence seq0, seq1, seq2, ..., seqm of nums, |seq1 - seq0| >= |seq2 - seq1| >= ... >= |seqm - seqm - 1|.

Return the length of such a subsequence.

Example 1:

Input: nums = [16,6,3]

Output: 3

Explanation: 

The longest subsequence is [16, 6, 3] with the absolute adjacent differences [10, 3].

Example 2:

Input: nums = [6,5,3,4,2,1]

Output: 4

Explanation:

The longest subsequence is [6, 4, 2, 1] with the absolute adjacent differences [2, 2, 1].

Example 3:

Input: nums = [10,20,10,19,10,20]

Output: 5

Explanation: 

The longest subsequence is [10, 20, 10, 19, 10] with the absolute adjacent differences [10, 10, 9, 9].

Constraints:

  • 2 <= nums.length <= 104
  • 1 <= nums[i] <= 300

Asked at 2 companies

Hints open one at a time

Hint 1
Use dynamic programming.
Hint 2
Store the maximum answer for each index and every possible difference.
Loading editor…
1class Solution:
2 def longestSubsequence(self, nums: List[int]) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
[16,6,3]
Input
[6,5,3,4,2,1]
Input
[10,20,10,19,10,20]
Your code is saved on this device.