Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Longest Increasing Subsequence II

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "2407" 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 integer array nums and an integer k. Find the longest subsequence of nums that meets the following requirements: The subsequence is strictly increasing and The difference between adjacent elements in the subsequence is at most k. Return the length of the longest subsequence that meets the requirements. A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements. Example 1: Input: nums = [4,2,1,4,3,4,5,8,15], k = 3 Output: 5 Explanation: The longest subsequence that meets the requirements is [1,3,4,5,8]. The subsequence has a length of 5, so we return 5. Note that the subsequence [1,3,4,5,8,15] does not meet the requirements because 15 - 8 = 7 is larger than 3. Example 2: Input: nums = [7,4,5,1,8,12,4,7], k = 5 Output: 4 Explanation: The longest subsequence that meets the requirements is [4,5,8,12]. The subsequence has a length of 4, so we return 4. Example 3: Input: nums = [1,5], k = 1 Output: 1 Explanation: The longest subsequence that meets the requirements is [1]. The subsequence has a length of 1, so we return 1. Constraints: 1 <= nums.length <= 105 1 <= nums[i], k <= 105

Explanation

Here's the breakdown of the solution:

  • Dynamic Programming with Range Queries: Use dynamic programming to store the length of the longest increasing subsequence ending at each number. Optimize transitions by using a data structure to efficiently query and update the maximum length within a range (numbers within k of the current number).
  • Binary Indexed Tree (Fenwick Tree): Employ a Binary Indexed Tree (BIT) to perform range maximum queries and point updates efficiently. BITs are well-suited for this type of problem, offering logarithmic time complexity for both operations.
  • Coordinate Compression (Optional, but Recommended): If the range of numbers in nums is very large, but the number of unique numbers is smaller, apply coordinate compression to reduce the memory footprint of the BIT. This is not strictly necessary to meet the constraints, but it can significantly improve performance if the input array contains very large numbers.

  • Time Complexity: O(n log m), where n is the length of nums and m is the maximum value in nums (or the number of distinct values after coordinate compression). Space Complexity: O(m) where m is the maximum value in nums.

Code

    def longest_subsequence(nums, k):
    """
    Finds the length of the longest increasing subsequence with adjacent element
    difference at most k.

    Args:
        nums: A list of integers.
        k: The maximum difference allowed between adjacent elements.

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

    max_val = max(nums)
    bit = [0] * (max_val + 1)

    def update(i, val):
        while i <= max_val:
            bit[i] = max(bit[i], val)
            i += i & -i

    def query(i):
        res = 0
        while i > 0:
            res = max(res, bit[i])
            i -= i & -i
        return res

    for num in nums:
        prev_max = query(max(0, num - 1)) # Find max subsequence length ending at value just before num
        current_len = prev_max + 1
        update(num, current_len)

    return query(max_val)

More from this blog

C

Chatmagic blog

2894 posts