Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Longest Subsequence With Decreasing Adjacent Difference

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "3409" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

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

Explanation

Here's a breakdown of the approach, followed by the Python code.

  • Dynamic Programming: We use dynamic programming to store the length of the longest valid subsequence ending at each index with a given last difference.
  • State Definition: dp[i][diff] stores the length of the longest subsequence ending at nums[i] with the absolute difference between the last two elements being diff.
  • Transition: For each nums[i], iterate through the previous elements nums[j] (where j < i). Calculate the absolute difference abs(nums[i] - nums[j]). If abs(nums[i] - nums[j]) <= diff, update dp[i][abs(nums[i] - nums[j])] using the value dp[j][diff] + 1.

  • Runtime & Storage Complexity: O(n2 m), where n is the length of the input array and m is the maximum possible difference (in this case, 300 - 1 = 299), and O(n m) respectively.

Code

    def longest_non_increasing_subsequence(nums):
    """
    Finds the length of the longest subsequence of nums such that the absolute
    differences between consecutive elements form a non-increasing sequence.

    Args:
        nums: A list of integers.

    Returns:
        The length of the longest subsequence.
    """

    n = len(nums)
    max_val = max(nums)
    min_val = min(nums)
    max_diff = max_val - min_val # maximum possible difference.  Can also just hardcode to 300, or 299.

    dp = [[1] * (max_diff + 1) for _ in range(n)]  # Initialize dp array. Every number itself is a sequence of length 1.

    max_len = 1

    for i in range(1, n):
        for j in range(i):
            diff = abs(nums[i] - nums[j])
            for k in range(diff, max_diff + 1):  # Iterate from diff up to max_diff
                dp[i][diff] = max(dp[i][diff], dp[j][k] + 1)
                max_len = max(max_len, dp[i][diff])

    return max_len

More from this blog

C

Chatmagic blog

2894 posts