Class Solution

  • All Implemented Interfaces:

    
    public final class Solution
    
                        

    3077 - Maximum Strength of K Disjoint Subarrays.

    Hard

    You are given a 0-indexed array of integers nums of length n, and a positive odd integer k.

    The strength of x subarrays is defined as strength = sum[1] * x - sum[2] * (x - 1) + sum[3] * (x - 2) - sum[4] * (x - 3) + ... + sum[x] * 1 where sum[i] is the sum of the elements in the <code>i<sup>th</sup></code> subarray. Formally, strength is sum of <code>(-1)<sup>i+1</sup> * sumi * (x - i + 1)</code> over all i's such that 1 &lt;= i &lt;= x.

    You need to select k disjoint subarrays from nums, such that their strength is maximum.

    Return the maximum possible strength that can be obtained.

    Note that the selected subarrays don't need to cover the entire array.

    Example 1:

    Input: nums = 1,2,3,-1,2, k = 3

    Output: 22

    Explanation: The best possible way to select 3 subarrays is: nums0..2, nums3..3, and nums4..4. The strength is (1 + 2 + 3) \* 3 - (-1) \* 2 + 2 \* 1 = 22.

    Example 2:

    Input: nums = 12,-2,-2,-2,-2, k = 5

    Output: 64

    Explanation: The only possible way to select 5 disjoint subarrays is: nums0..0, nums1..1, nums2..2, nums3..3, and nums4..4. The strength is 12 \* 5 - (-2) \* 4 + (-2) \* 3 - (-2) \* 2 + (-2) \* 1 = 64.

    Example 3:

    Input: nums = -1,-2,-3, k = 1

    Output: -1

    Explanation: The best possible way to select 1 subarray is: nums0..0. The strength is -1.

    Constraints:

    • <code>1 <= n <= 10<sup>4</sup></code>

    • <code>-10<sup>9</sup><= numsi<= 10<sup>9</sup></code>

    • 1 &lt;= k &lt;= n

    • <code>1 <= n * k <= 10<sup>6</sup></code>

    • k is odd.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      Solution()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final Long maximumStrength(IntArray n, Integer k)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait