Advertisement

Subarrays with K Different Integers - LeetCode 992 Solution

Subarrays with K Different Integers - Complete Solution Guide

Subarrays with K Different Integers is LeetCode problem 992, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given an integer array nums and an integer k , return the number of good subarrays of nums . A good array is an array where the number of different integers in that array is exactly k . For example, [1,2,3,1,2] has 3 different integers: 1 , 2 , and 3 . A subarray is a contiguous part of an array. Example 1: Input: nums = [1,2,1,2,3], k = 2 Output: 7 Explanation: 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] Example 2: Input: nums = [1

Detailed Explanation

The problem asks us to find the number of subarrays within a given integer array `nums` that contain exactly `k` distinct integers. A subarray is a contiguous section of the array. For example, given `nums = [1, 2, 1, 2, 3]` and `k = 2`, we need to identify all subarrays that have exactly 2 distinct numbers. The goal is to count how many such subarrays exist. The constraints specify the size of the input array (up to 2 * 10^4) and the range of values within the array (1 to the length of the array), as well as the range for K (1 to the length of the array)

Solution Approach

The provided code utilizes a sliding window approach to count the number of subarrays with 'at most' K distinct integers. It calculates this count for K and K-1, then subtracts the latter from the former to get the desired result. The 'atMostK' function maintains a sliding window and a counter for distinct elements. As the right pointer of the window moves, the counter is updated. If the number of distinct elements exceeds K, the left pointer of the window moves until the condition is met again. The number of valid subarrays ending at each right pointer is added to a running total.

Step-by-Step Algorithm

  1. Step 1: Implement the `atMostK` helper function. This function counts the number of subarrays with at most `K` distinct integers.
  2. Step 2: Initialize a hash map `counts` to store the frequency of each number within the current sliding window.
  3. Step 3: Initialize the left pointer `left` to 0 and the result counter `res` to 0.
  4. Step 4: Iterate through the input array `nums` with the right pointer `right`.
  5. Step 5: For each number `num` at the right pointer, update its frequency in the `counts` map. If the number is encountered for the first time (frequency becomes 1), decrement `K`.
  6. Step 6: While `K` is less than 0, it means the current window has more than `K` distinct integers. Shrink the window from the left by incrementing `left` and decrementing the frequency of the number at the left pointer in the `counts` map. If the frequency of the number at the left pointer becomes 0, increment `K` (as we've removed a distinct integer from the window).
  7. Step 7: After ensuring that the current window has at most `K` distinct integers, add `right - left + 1` to the result counter `res`. This represents the number of valid subarrays ending at the current right pointer.
  8. Step 8: Once the loop finishes, return the `res` from `atMostK` function.
  9. Step 9: In the main function `subarraysWithKDistinct`, call `atMostK(k) - atMostK(k - 1)` to get the desired result. This calculates the number of subarrays with exactly `k` distinct integers.

Key Insights

  • Insight 1: The problem can be solved efficiently using the sliding window technique.
  • Insight 2: The number of subarrays with exactly K distinct integers can be found by subtracting the number of subarrays with at most K-1 distinct integers from the number of subarrays with at most K distinct integers. This simplifies the problem to finding subarrays with 'at most' a certain number of distinct elements.
  • Insight 3: A hash table (or dictionary/map) is essential for efficiently tracking the frequency of elements within the sliding window.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Array, Hash Table, Sliding Window, Counting.

Companies

Asked at: Morgan Stanley, Roblox.