java.lang.Object
g0901_1000.s0992_subarrays_with_k_different_integers.Solution

public class Solution extends Object
992 - Subarrays with K Different Integers.<p>Hard</p> <p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the number of <strong>good subarrays</strong> of</em> <code>nums</code>.</p> <p>A <strong>good array</strong> is an array where the number of different integers in that array is exactly <code>k</code>.</p> <ul> <li>For example, <code>[1,2,3,1,2]</code> has <code>3</code> different integers: <code>1</code>, <code>2</code>, and <code>3</code>.</li> </ul> <p>A <strong>subarray</strong> is a <strong>contiguous</strong> part of an array.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [1,2,1,2,3], k = 2</p> <p><strong>Output:</strong> 7</p> <p><strong>Explanation:</strong> Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2]</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [1,2,1,3,4], k = 3</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= nums.length <= 2 * 10<sup>4</sup></code></li> <li><code>1 <= nums[i], k <= nums.length</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • subarraysWithKDistinct

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