Skip to main content

Maximum Score Using Exactly K Pairs - Video Solutions

HardArrayDynamic Programming

Maximum Score Using Exactly K Pairs šŸ”„ LeetCode 3836 | Weekly Contest 488 Q4 | DP + Recursion + Memo

Study Placement
20:48391 views
10 video solutions available

Maximum Score Using Exactly K Pairs - Video Solution

Watch 10 video solutions for Maximum Score Using Exactly K Pairs, a hard level problem involving Array, Dynamic Programming. This walkthrough by Study Placement has 391 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

Problem Statement

You are given two integer arrays nums1 and nums2 of lengths n and m respectively, and an integer k.

You must choose exactly k pairs of indices (i1, j1), (i2, j2), ..., (ik, jk) such that:

  • 0 <= i1 < i2 < ... < ik < n
  • 0 <= j1 < j2 < ... < jk < m

For each chosen pair (i, j), you gain a score of nums1[i] * nums2[j].

The total score is the sum of the products of all selected pairs.

Return an integer representing the maximum achievable total score.

 

Example 1:

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

Output: 22

Explanation:

One optimal choice of index pairs is:

  • (i1, j1) = (1, 0) which scores 3 * 4 = 12
  • (i2, j2) = (2, 1) which scores 2 * 5 = 10

This gives a total score of 12 + 10 = 22.

Example 2:

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

Output: 26

Explanation:

One optimal choice of index pairs is:

  • (i1, j1) = (0, 0) which scores -2 * -3 = 6
  • (i2, j2) = (2, 1) which scores 5 * 4 = 20

The total score is 6 + 20 = 26.

Example 3:

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

Output: -7

Explanation:

The optimal choice of index pairs is:

  • (i1, j1) = (0, 0) which scores -3 * 1 = -3
  • (i2, j2) = (1, 1) which scores -2 * 2 = -4

The total score is -3 + (-4) = -7.

 

Constraints:

  • 1 <= n == nums1.length <= 100
  • 1 <= m == nums2.length <= 100
  • -106 <= nums1[i], nums2[i] <= 106
  • 1 <= k <= min(n, m)
Read full problem with examples

Approach Overview

Problem Overview: You are given an array and must form exactly k disjoint pairs. Each pair contributes a score based on the two selected elements. The goal is to maximize the total score while ensuring every element is used at most once.

Approach 1: Brute Force Pair Enumeration (Exponential Time, O(n^(2k)) time, O(k) space)

The most direct strategy tries every possible way to choose k disjoint pairs. You recursively pick two unused elements, add their score, and continue forming the remaining pairs. This guarantees the optimal answer because every configuration is explored. However, the number of pair combinations grows extremely fast as n increases, making this approach impractical beyond small inputs. It is mainly useful for understanding the search space before optimizing.

Approach 2: Dynamic Programming with Prefix Decisions (O(n² · k) time, O(n · k) space)

The efficient solution uses dynamic programming to avoid recomputing overlapping subproblems. First process the array from left to right and define dp[i][p] as the maximum score achievable using the first i elements while forming exactly p pairs. For each position i, you have two choices: skip the element (carry forward dp[i-1][p]) or pair it with a previous element j. If you pair j and i, add their pair score and transition from dp[j-1][p-1]. Iterating over possible partners ensures all valid pairings are considered while maintaining disjoint usage of elements.

This DP effectively converts an exponential pairing problem into a structured state transition. The outer loop iterates through the array, the inner loop checks potential pairing partners, and the pair count dimension ensures exactly k pairs are formed. Because states reuse previously computed results, the total complexity becomes manageable even for large inputs.

Recommended for interviews: Start by explaining the brute force pairing idea to demonstrate understanding of the constraints and why the search space explodes. Then move to the dynamic programming formulation with dp[i][p]. Interviewers typically expect the DP optimization because it shows you can convert a combinatorial pairing problem into a structured state transition with clear time and space bounds.

Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Pair EnumerationO(n^(2k))O(k)Useful for understanding the search space or validating small test cases
Dynamic Programming (Prefix States)O(n^2 * k)O(n * k)General optimal approach for large arrays and interview settings