LeetCode 2542, Medium. Topics: Array, Greedy, Sorting, Heap (Priority Queue). View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 16 parametrized pytest cases, and a playground notebook:
Problem
You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.
<p>For chosen indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, …, <code>i<sub>k - 1</sub></code>, your <strong>score</strong> is defined as:</p>
<ul>
<li>The sum of the selected elements from <code>nums1</code> multiplied with the <strong>minimum</strong> of the selected elements from <code>nums2</code>.</li>
<li>It can defined simply as: <code>(nums1[i<sub>0</sub>] + nums1[i<sub>1</sub>] +…+ nums1[i<sub>k - 1</sub>]) * min(nums2[i<sub>0</sub>] , nums2[i<sub>1</sub>], … ,nums2[i<sub>k - 1</sub>])</code>.</li>
</ul>
<p>Return <em>the <strong>maximum</strong> possible score.</em></p>
<p>A <strong>subsequence</strong> of indices of an array is a set that can be derived from the set <code>{0, 1, …, n-1}</code> by deleting some or no elements.</p>
Examples
Constraints
- n == nums1.length == nums2.length
- 1 <= n <= 10^5
- 0 <= nums1[i], nums2[j] <= 10^5
- 1 <= k <= n
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
NeetCode All. Last modified on September 7, 2026