LeetCode 3428: Maximum and Minimum Sums of at Most Size K Subsequences Solution

Master LeetCode problem 3428 (Maximum and Minimum Sums of at Most Size K Subsequences), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

3428. Maximum and Minimum Sums of at Most Size K Subsequences

Problem Explanation

Explanation

To solve this problem, we can use the concept of dynamic programming. We will create two DP arrays - one to store the maximum sum and another to store the minimum sum of subsequences of at most size k. We will then iterate over the elements of the input array nums and update these DP arrays accordingly. Finally, we can calculate the total sum by summing up the maximum and minimum sums of all subsequences.

  • Initialize two DP arrays dpMax and dpMin with sizes 2^k.
  • For each element in nums, update the DP arrays based on the current element's value and the previous values in the DP arrays.
  • Calculate the total sum by summing up the values in dpMax and dpMin arrays.
  • Return the total sum modulo 10^9 + 7 as the final answer.

Time complexity: O(n * 2^k), where n is the length of the input array nums. Space complexity: O(2^k)

Solution Code

class Solution {
    public int minMaxSum(int[] nums, int k) {
        int mod = 1000000007;
        int n = nums.length;
        int[] dpMax = new int[1 << k];
        int[] dpMin = new int[1 << k];
        
        for (int num : nums) {
            int[] nextDpMax = dpMax.clone();
            int[] nextDpMin = dpMin.clone();
            
            for (int i = 0; i < (1 << k); i++) {
                int j = i | (1 << (k - 1));
                nextDpMax[j] = Math.max(dpMax[i] + num, nextDpMax[j]);
                nextDpMax[i] = Math.max(dpMax[i], nextDpMax[i]);
                
                nextDpMin[j] = Math.min(dpMin[i] + num, nextDpMin[j]);
                nextDpMin[i] = Math.min(dpMin[i], nextDpMin[i]);
            }
            
            dpMax = nextDpMax;
            dpMin = nextDpMin;
        }
        
        int totalSum = 0;
        for (int i = 0; i < (1 << k); i++) {
            totalSum = (totalSum + dpMax[i] + dpMin[i]) % mod;
        }
        
        return totalSum;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3428 (Maximum and Minimum Sums of at Most Size K Subsequences)?

This page provides optimized solutions for LeetCode problem 3428 (Maximum and Minimum Sums of at Most Size K Subsequences) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 3428 (Maximum and Minimum Sums of at Most Size K Subsequences)?

The time complexity for LeetCode 3428 (Maximum and Minimum Sums of at Most Size K Subsequences) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3428 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3428 in Java, C++, or Python.

Back to LeetCode Solutions