Advertisement

Find the Maximum Length of a Good Subsequence II - LeetCode 3177 Solution

Find the Maximum Length of a Good Subsequence II - Complete Solution Guide

Find the Maximum Length of a Good Subsequence II is LeetCode problem 3177, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given an integer array nums and a non-negative integer k . A sequence of integers seq is called good if there are at most k indices i in the range [0, seq.length - 2] such that seq[i] != seq[i + 1] . Return the maximum possible length of a good subsequence of nums . Example 1: Input: nums = [1,2,1,1,3], k = 2 Output: 4 Explanation: The maximum length subsequence is [ 1 , 2 , 1 , 1 ,3] . Example 2: Input: nums = [1,2,3,4,5,1], k = 0 Output: 2 Explanation: The maximum length subsequence is

Detailed Explanation

The problem requires us to find the maximum length of a "good" subsequence from a given integer array `nums`. A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements. A subsequence is considered "good" if it has at most `k` indices `i` where `seq[i] != seq[i+1]`. In other words, a good subsequence can have at most `k` adjacent pairs of different numbers. The input consists of an integer array `nums` and a non-negative integer `k`. The output is the maximum possible length of a good subsequence of `nums`. The constraints are: the length of `nums` is at most 5000, each element of `nums` is between 1 and 10^9, and `k` is between 0 and the minimum of 50 and the length of `nums`.

Solution Approach

The solution utilizes dynamic programming to find the maximum length of a good subsequence. It maintains a `dp` table (represented as a HashMap in Java and unordered_map in C++, and a 2D array in C) where `dp[num][j]` stores the maximum length of a good subsequence ending with `num` and having at most `j` differences. In addition to the DP table, the algorithm keeps track of the maximum (`max1`) and second maximum (`max2`) subsequence lengths for each value of `j` to efficiently compute the lengths of new subsequences. When processing a new number `num`, it updates the `dp` table by considering two options: extending an existing subsequence with the same last element or extending a subsequence with a different last element. The maximum and second maximum values are then updated for each `j` value.

Step-by-Step Algorithm

  1. Step 1: Initialize the DP table `dp` to store the maximum length of subsequences ending with a particular number and number of differences.
  2. Step 2: Initialize arrays `max1`, `val1`, and `max2` to track the maximum and second maximum subsequence lengths for each value of `j` (number of differences).
  3. Step 3: Iterate through each number `num` in the input array `nums`.
  4. Step 4: For each `num`, create a new array `new_lens_for_num` to store the updated subsequence lengths.
  5. Step 5: Update `new_lens_for_num[0]` (the case when j = 0, i.e. no differences allowed) as 1 + `dp[num][0]`
  6. Step 6: Iterate through `j` from 1 to `k`.
  7. Step 7: Calculate `len_same`: the length obtained by extending a subsequence ending with `num` with the current number `num`.
  8. Step 8: Calculate `len_diff`: the length obtained by extending a subsequence ending with a different number. This requires looking up the maximum subsequence length from the previous `j-1` differences, taking care to avoid extending with the same number again by using `max2` when necessary.
  9. Step 9: Store the maximum of `len_same` and `len_diff` in `new_lens_for_num[j]`.
  10. Step 10: Update `dp[num]` with `new_lens_for_num`.
  11. Step 11: Update `max1`, `val1`, and `max2` based on the new subsequence lengths. If `new_lens_for_num[j]` is greater than the current `max1[j]`, update `max2[j]` with the old `max1[j]` (if the number is different), and set `max1[j]` to `new_lens_for_num[j]` and `val1[j]` to `num`. If `new_lens_for_num[j]` is greater than `max2[j]` but not `max1[j]` (and the number is different), update `max2[j]`.
  12. Step 12: After iterating through all numbers in `nums`, return the maximum value in `max1`.

Key Insights

  • Insight 1: Dynamic Programming is suitable as we are trying to find the maximum length by building up solutions from subproblems.
  • Insight 2: We need to track the maximum length subsequence ending with a particular number and the number of differences encountered (up to `k`).
  • Insight 3: Maintaining both the maximum length and the second maximum length for each value of `k` allows efficient updating of DP states when the current number is equal to the previous maximum's number.

Complexity Analysis

Time Complexity: O(n*k)

Space Complexity: O(n*k)

Topics

This problem involves: Array, Hash Table, Dynamic Programming.

Companies

Asked at: Snowflake.