3409. Longest Subsequence With Decreasing Adjacent Difference Medium

@problem@discussion
#Array#Dynamic Programming



1/**
2 * [3409] Longest Subsequence With Decreasing Adjacent Difference
3 *
4 * You are given an array of integers nums.
5 * Your task is to find the length of the longest <span data-keyword="subsequence-array">subsequence</span> 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|.
6 * Return the length of such a subsequence.
7 *  
8 * <strong class="example">Example 1:
9 * <div class="example-block">
10 * Input: <span class="example-io">nums = [16,6,3]</span>
11 * Output: <span class="example-io">3</span>
12 * Explanation: 
13 * The longest subsequence is [16, 6, 3] with the absolute adjacent differences [10, 3].
14 * </div>
15 * <strong class="example">Example 2:
16 * <div class="example-block">
17 * Input: <span class="example-io">nums = [6,5,3,4,2,1]</span>
18 * Output: <span class="example-io">4</span>
19 * Explanation:
20 * The longest subsequence is [6, 4, 2, 1] with the absolute adjacent differences [2, 2, 1].
21 * </div>
22 * <strong class="example">Example 3:
23 * <div class="example-block">
24 * Input: <span class="example-io">nums = [10,20,10,19,10,20]</span>
25 * Output: <span class="example-io">5</span>
26 * Explanation: 
27 * The longest subsequence is [10, 20, 10, 19, 10] with the absolute adjacent differences [10, 10, 9, 9].
28 * </div>
29 *  
30 * Constraints:
31 * 
32 * 	2 <= nums.length <= 10^4
33 * 	1 <= nums[i] <= 300
34 * 
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/longest-subsequence-with-decreasing-adjacent-difference/
39// discuss: https://leetcode.com/problems/longest-subsequence-with-decreasing-adjacent-difference/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43impl Solution {
44    pub fn longest_subsequence(nums: Vec<i32>) -> i32 {
45        0
46    }
47}
48
49// submission codes end
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn test_3409() {
57    }
58}
59


Back
© 2025 bowen.ge All Rights Reserved.