Skip to content

Latest commit

 

History

History
99 lines (72 loc) · 4.33 KB

2542.Maximum_Subsequence_Score(Medium).md

File metadata and controls

99 lines (72 loc) · 4.33 KB

2542. Maximum Subsequence Score (Medium)

Date and Time: Nov 10, 2024, 12:15 (EST)

Link: https://leetcode.com/problems/maximum-subsequence-score/


Question:

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[i0] + nums1[i1] +...+ nums1[ik - 1]) * min(nums2[i0] , nums2[i1], ... ,nums2[ik - 1]).

Return the maximum possible score.

A subsequence of indices of an array is a set that can be derived from the set {0, 1, ..., n-1} by deleting some or no elements.


Example 1:

Input: nums1 = [1,3,3,2], nums2 = [2,1,3,4], k = 3

Output: 12

Explanation:
The four possible subsequence scores are:
- We choose the indices 0, 1, and 2 with score = (1+3+3) * min(2,1,3) = 7.
- We choose the indices 0, 1, and 3 with score = (1+3+2) * min(2,1,4) = 6.
- We choose the indices 0, 2, and 3 with score = (1+3+2) * min(2,3,4) = 12.
- We choose the indices 1, 2, and 3 with score = (3+3+2) * min(1,3,4) = 8.
Therefore, we return the max score, which is 12.

Example 2:

Input: nums1 = [4,2,3,1,1], nums2 = [7,5,10,9,6], k = 1

Output: 30

Explanation:
Choosing index 2 is optimal: nums1[2] * nums2[2] = 3 * 10 = 30 is the maximum possible score.


Constraints:

  • n == nums1.length == nums2.length

  • 1 <= n <= 10^5

  • 0 <= nums1[i], nums2[j] <= 10^5

  • 1 <= k <= n


Walk-through:

  1. We merged nums2, nums1 together and we sort it by nums2 in descending order.

  2. We repeatedly add v1 into minHeap[], when len(minHeap) == k, we update res = max(res, curSum * v2), then heappop() the samllest v1 from minHeap.


Python Solution:

class Solution:
    def maxScore(self, nums1: List[int], nums2: List[int], k: int) -> int:
        # nums1: [3,1,3,2], nums2: [1,2,3,4], k = 3
        # (1+3+2) * 2 = 12

        # nums1: [2,1,4,1,3], nums2: [5,6,7,9,10], k = 1
        # 3 * 10 = 30

        # nums1: [12,1,2,14], nums2: [6,7,11,13], k = 3
        # (12+14+2) * 6 = 168

        # merged them together [nums2[i], nums1[i]] descending otder
        # maintain a heap of len(k) with nums1, pop the smallest val 
        # if len(heap) == k: udpate res with new max val

        # TC: O(nlog k), SC: O(n + k)
        res, minHeap = 0, []
        merged = [(nums2[i], nums1[i]) for i in range(len(nums1))]
        merged.sort(reverse = True)     # Sort in descending order
        curSum = 0
        for v2, v1 in merged:
            curSum += v1
            heapq.heappush(minHeap, v1)
            if len(minHeap) == k:
                res = max(res, curSum * v2)
                curSum -= heapq.heappop(minHeap)
        return res

Time Complexity: $O(nlog\ k)$, n is how many elements in nums1, k is how many indices we can choose. We traverse n elements in the for loop, and each time we call heappop() and heappush(), which take $O(log\ k)$.
Space Complexity: $O(n + k)$, create a merged array of length n and minHeap with length k.


CC BY-NC-SABY: credit must be given to the creatorNC: Only noncommercial uses of the work are permittedSA: Adaptations must be shared under the same terms