Skip to content
Problem 2542 · LeetCode 2542(opens in a new tab)

Maximum Subsequence Score

MediumGreedy algorithms

You get two arrays of the same length n, nums1 and nums2, and a number k. Pick exactly k different indexes.

The score of a pick is the sum of nums1 at those indexes, multiplied by the smallest nums2 at those indexes. Return the highest score any pick can reach.

Example 1

Input:  nums1 = [5, 2, 6, 1], nums2 = [2, 6, 3, 4], k = 2
Output: 24
Why:    indexes 1 and 2: (2 + 6) × min(6, 3) = 8 × 3
2 more examples and the constraints

Example 2

Input:  nums1 = [3, 1, 2], nums2 = [2, 9, 4], k = 1
Output: 9
Why:    index 1 alone scores 1 × 9

Example 3

Input:  nums1 = [1, 2, 3], nums2 = [4, 5, 6], k = 3
Output: 24
Why:    all three must be picked: (1 + 2 + 3) × 4

Constraints

  • 1 ≤ n ≤ 100,000, and both arrays have length n
  • 0 ≤ nums1[i], nums2[i] ≤ 100,000
  • 1 ≤ k ≤ n
  • The score can exceed 32 bits, so use 64-bit integers

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Fix the minimum, sort the restO(n² log n)
  2. Sort by nums2 and keep a min-heapO(n log n)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for 100,000 indexesVerdict
Fix the minimum, sort the restO(n² log n)
Too slow; re-sorts for every index
Sort and keep a min-heapO(n log n)
Best; one sort, one heap

n = length of the arrays, k = how many indexes are picked.

Edge cases to test

  • k = 1. The score of one index is nums1[i] × nums2[i]; the heap holds one value and pops every other step.
  • k = n. Every index must be picked, so there is a single score: the whole sum times the smallest nums2.
  • Zeros. A zero in nums2 makes every pick containing it score 0, and the answer can be 0 when nums1 is all zeros.
  • Overflow. 100,000 values of 100,000, times 100,000, is 10¹⁵: keep total and the score in 64-bit integers.
  • Ties in nums2. Equal nums2 values can be sorted in any order; the scores computed are still all real picks or lower.

Common mistakes

  • Sorting by nums1 instead of nums2, which leaves the minimum changing in both directions.
  • Scoring before the heap holds k values, which counts picks with fewer than k indexes.
  • Using a 32-bit int for the sum or the product.

How to talk through it

  1. 1Clarify. Exactly k indexes, not at most k? Can values be zero? How large can the score get?
  2. 2Fix the minimum. If I decide which nums2 is the minimum, the rest of the pick is easy: the largest nums1 among indexes with a larger nums2.
  3. 3Sort to make that easy. Sorted by nums2 from high to low, the allowed partners are just everything seen so far.
  4. 4Keep the top k. A min-heap of size k gives the k largest nums1 and their sum in O(log k) per index.
  5. 5Test out loud. Run k = 1, k = n, and a case where the largest sum has a tiny minimum.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?