java.lang.Object
g2401_2500.s2461_maximum_sum_of_distinct_subarrays_with_length_k.Solution

public class Solution extends Object
2461 - Maximum Sum of Distinct Subarrays With Length K.<p>Medium</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. Find the maximum subarray sum of all the subarrays of <code>nums</code> that meet the following conditions:</p> <ul> <li>The length of the subarray is <code>k</code>, and</li> <li>All the elements of the subarray are <strong>distinct</strong>.</li> </ul> <p>Return <em>the maximum subarray sum of all the subarrays that meet the conditions</em>_._ If no subarray meets the conditions, return <code>0</code>.</p> <p><em>A <strong>subarray</strong> is a contiguous non-empty sequence of elements within an array.</em></p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [1,5,4,2,9,9,9], k = 3</p> <p><strong>Output:</strong> 15</p> <p><strong>Explanation:</strong> The subarrays of nums with length 3 are:</p> <ul> <li> <p>[1,5,4] which meets the requirements and has a sum of 10.</p> </li> <li> <p>[5,4,2] which meets the requirements and has a sum of 11.</p> </li> <li> <p>[4,2,9] which meets the requirements and has a sum of 15.</p> </li> <li> <p>[2,9,9] which does not meet the requirements because the element 9 is repeated.</p> </li> <li> <p>[9,9,9] which does not meet the requirements because the element 9 is repeated.</p> </li> </ul> <p>We return 15 because it is the maximum subarray sum of all the subarrays that meet the conditions</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [4,4,4], k = 3</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> The subarrays of nums with length 3 are:</p> <ul> <li>[4,4,4] which does not meet the requirements because the element 4 is repeated. We return 0 because no subarrays meet the conditions.</li> </ul> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= k <= nums.length <= 10<sup>5</sup></code></li> <li><code>1 <= nums[i] <= 10<sup>5</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • maximumSubarraySum

      public long maximumSubarraySum(int[] nums, int k)