java.lang.Object
g2501_2600.s2547_minimum_cost_to_split_an_array.Solution

public class Solution extends Object
2547 - Minimum Cost to Split an Array.<p>Hard</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>Split the array into some number of non-empty subarrays. The <strong>cost</strong> of a split is the sum of the <strong>importance value</strong> of each subarray in the split.</p> <p>Let <code>trimmed(subarray)</code> be the version of the subarray where all numbers which appear only once are removed.</p> <ul> <li>For example, <code>trimmed([3,1,2,4,3,4]) = [3,4,3,4].</code></li> </ul> <p>The <strong>importance value</strong> of a subarray is <code>k + trimmed(subarray).length</code>.</p> <ul> <li>For example, if a subarray is <code>[1,2,3,3,3,4,4]</code>, then trimmed(<code>[1,2,3,3,3,4,4]) = [3,3,3,4,4].</code>The importance value of this subarray will be <code>k + 5</code>.</li> </ul> <p>Return <em>the minimum possible cost of a split of</em> <code>nums</code>.</p> <p>A <strong>subarray</strong> is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [1,2,1,2,1,3,3], k = 2</p> <p><strong>Output:</strong> 8</p> <p><strong>Explanation:</strong> We split nums to have two subarrays: [1,2], [1,2,1,3,3]. &rsquo;</p> <p>The importance value of [1,2] is 2 + (0) = 2.</p> <p>The importance value of [1,2,1,3,3] is 2 + (2 + 2) = 6.</p> <p>The cost of the split is 2 + 6 = 8. It can be shown that this is the minimum possible cost among all the possible splits.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [1,2,1,2,1], k = 2</p> <p><strong>Output:</strong> 6</p> <p><strong>Explanation:</strong> We split nums to have two subarrays: [1,2], [1,2,1].</p> <p>The importance value of [1,2] is 2 + (0) = 2.</p> <p>The importance value of [1,2,1] is 2 + (2) = 4.</p> <p>The cost of the split is 2 + 4 = 6. It can be shown that this is the minimum possible cost among all the possible splits.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> nums = [1,2,1,2,1], k = 5</p> <p><strong>Output:</strong> 10</p> <p><strong>Explanation:</strong> We split nums to have one subarray: [1,2,1,2,1].</p> <p>The importance value of [1,2,1,2,1] is 5 + (3 + 2) = 10.</p> <p>The cost of the split is 10. It can be shown that this is the minimum possible cost among all the possible splits.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= nums.length <= 1000</code></li> <li><code>0 <= nums[i] < nums.length</code></li> <li><code>1 <= k <= 10<sup>9</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • minCost

      public int minCost(int[] nums, int k)