Maximum Subsequence Score - Complete Solution Guide
Maximum Subsequence Score is LeetCode problem 2542, a Medium 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 two 0-indexed integer arrays nums1 and nums2 of equal length n and a positive integer k . You must choose a subsequence of indices from nums1 of length k . For chosen indices i 0 , i 1 , ..., i k - 1 , your score is defined as: The sum of the selected elements from nums1 multiplied with the minimum of the selected elements from nums2 . It can defined simply as: (nums1[i 0 ] + nums1[i 1 ] +...+ nums1[i k - 1 ]) * min(nums2[i 0 ] , nums2[i 1 ], ... ,nums2[i k - 1 ]) . Return the maxi
Detailed Explanation
The problem asks us to find the maximum possible score achievable by selecting a subsequence of length `k` from `nums1`, where the score is defined as the sum of the selected elements from `nums1` multiplied by the minimum of the selected elements from `nums2`. We are given two arrays, `nums1` and `nums2`, of equal length `n`, and an integer `k`. We need to select `k` indices and calculate the score for each possible subsequence and return the maximum score among them.
Solution Approach
The solution uses a greedy approach combined with sorting and a min-heap. First, we create pairs of (nums1[i], nums2[i]) and sort them in descending order based on the values in `nums2`. This allows us to consider potential minimums of `nums2` in decreasing order. Then, we iterate through the sorted pairs, maintaining a min-heap of size `k` to store the `nums1` values encountered so far. The `current_sum` variable stores the sum of elements in the min-heap. At each step, the algorithm updates the `max_score` by considering the current element's `nums2` value as the minimum and `current_sum` as the sum of corresponding `nums1` values of the k largest `nums1` values so far.
Step-by-Step Algorithm
- Step 1: Create an array of pairs (nums1[i], nums2[i]).
- Step 2: Sort the pairs in descending order based on the nums2 values.
- Step 3: Initialize a min-heap, current_sum, and max_score to 0.
- Step 4: Iterate through the sorted pairs:
- Step 5: Add the current nums1 value to the min-heap and update the current_sum.
- Step 6: If the min-heap size exceeds k, remove the smallest element from the heap and subtract it from current_sum.
- Step 7: If the min-heap size is equal to k, calculate the current score (current_sum * current nums2 value) and update max_score if the current score is greater.
- Step 8: Return the max_score.
Key Insights
- Insight 1: Sorting the pairs (nums1[i], nums2[i]) based on nums2[i] in descending order allows us to iterate through potential minimum values for nums2 and greedily select the largest nums1 values.
- Insight 2: Using a min-heap of size k to track the k largest nums1 values encountered so far ensures that at any point, we have the k largest nums1 elements for a given minimum value (nums2[i]).
- Insight 3: The core idea is that we iterate through the `nums2` values from largest to smallest. Each value we encounter is a potential minimum for our subsequence. We then want to maximize the sum of `nums1` values included in the subsequence, given that `nums2[i]` is the minimum. Maintaining a min-heap of the largest `k` `nums1` values allows us to efficiently update the sum.
Complexity Analysis
Time Complexity: O(n log n), where n is the length of the arrays. Sorting the pairs takes O(n log n) time. The iteration through the pairs takes O(n) time, and each heap operation takes O(log k) time. However, since we are performing heap operation at most n times, the time complexity contributed by the heap operations is O(n log k). Since k <= n, O(n log k) <= O(n log n). Therefore, the dominant factor is the sorting step making the total time complexity O(n log n).
Space Complexity: O(n), where n is the length of the arrays. The pairs array takes O(n) space. The min-heap takes O(k) space, and since k <= n, the heap space is bounded by O(n).
Topics
This problem involves: Array, Greedy, Sorting, Heap (Priority Queue).
Companies
Asked at: DE Shaw.