Problem solution · C++

Maximum Score Using Exactly K Pairs

Maximum Score Using Exactly K Pairs: a C++ solution using dynamic programming. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Dynamic programming
Source
Kamyu LeetCode Solutions
Length
63 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Dynamic programming

For Maximum Score Using Exactly K Pairs, the implementation records answers for smaller states and reuses them to build the requested result without repeating work.

  1. Define precisely what one DP state represents.
  2. Establish the base cases before transitions are evaluated.
  3. Process states in dependency order and combine only already-known values.

Code notes

  • 63 lines of C++ from the credited upstream file maximum-score-using-exactly-k-pairs.cpp.
  • The implementation visibly relies on sequence storage, cached states.
  • 8 loop blocks detected.

Complexity

Multiply the number of reachable states by the work performed for each transition, then include the stored state table in memory usage.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeMaximum Score Using Exactly K Pairs · C++C++
Use this to learn the idea, then write your own version.
// Time:  O(n * m * k)// Space: O(min(n, m) * k) // dpclass Solution {public:    long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {        static const auto& NEG_INF = numeric_limits<int64_t>::min();                if (size(nums1) < size(nums2)) {            swap(nums1, nums2);        }        vector<vector<int64_t>> dp(size(nums2) + 1, vector<int64_t>(k + 1, NEG_INF));        for (int j = 0; j < size(dp); ++j) {            dp[j][0] = 0;        }        vector<vector<int64_t>> new_dp(size(nums2) + 1, vector<int64_t>(k + 1, NEG_INF));        for (int i = 0; i < size(nums1); ++i) {            for (int j = 0; j < size(new_dp); ++j) {                new_dp[j][0] = 0;            }                        for (int j = 0; j < size(nums2); ++j) {                const auto& score = static_cast<int64_t>(nums1[i]) * nums2[j];                const auto& mx = min({i + 1, j + 1, k});                for (int c = 0; c < mx; ++c) {                    new_dp[j + 1][c + 1] = max({                        new_dp[j][c + 1],                        dp[j + 1][c + 1],                        dp[j][c] + score                    });                }            }            swap(dp, new_dp);        }        return dp.back().back();    }}; // Time:  O(n * m * k)// Space: O(n * m)class Solution2 {public:    long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {        static const auto& NEG_INF = numeric_limits<int64_t>::min();         vector<vector<int64_t>> dp(size(nums1), vector<int64_t>(size(nums2), NEG_INF));        vector<vector<int64_t>> new_dp(size(nums1), vector<int64_t>(size(nums2), NEG_INF));        for (int c = 0; c < k; ++c) {            for (int i = c; i < size(nums1); ++i) {                for (int j = c; j < size(nums2); ++j) {                    new_dp[i][j] = max({                        j - 1 >= c ? new_dp[i][j - 1] : NEG_INF,                        i - 1 >= c ? new_dp[i - 1][j] : NEG_INF,                        (c ? dp[i - 1][j - 1] : 0) + static_cast<int64_t>(nums1[i]) * nums2[j]                    });                }            }            swap(dp, new_dp);        }        return dp.back().back();    }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗