Talentd
FleetCode
Make and follow sheets.Sign in

Count Frequency Staircase Subarrays II solution and explanation

Hard4 min read16.5% acceptanceUpdated 26 Oct 2026
Ultra Hard💀DSA Question asked by Leetcode in Weekly Contest 525(Q4,4095)Kumar K [Amazon] · 50:24 · 547 viewsWatch on YouTube
1 more video on this problem
  1. Ultra Hard💀DSA Question asked by Leetcode in Weekly Contest 525(Q4,4095)Kumar K [Amazon] · 50:24 · 547 views
  2. Leetcode 4095 | Count Frequency Staircase Subarrays II | Leetcode weekly contest 525CodeWithMeGuys · 32:16 · 377 views

The problem

You are given an integer array nums.

Consider the distinct values of a subarray in the order in which they first appear.

If there are k distinct values, the subarray is called a frequency staircase if the first distinct value occurs exactly once, the second distinct value occurs exactly twice, and so on, with the kth distinct value occurring exactly k times.

Equivalently, the frequencies of the distinct values, in the order of their first occurrence, must be exactly 1, 2, 3, ..., k.

Return the number of frequency staircase subarrays in nums.

A subarray is a contiguous non-empty sequence of elements within an array.

The frequency of a value in a subarray is the number of times it occurs in that subarray.

Example 1:

Input: nums = [5,8,8,2,2,2]

Output: 9

Explanation:

Every single-element subarray has frequencies [1], and there are 6 of them. 3 longer subarrays also qualify:

Subarray Values by first appearance Frequencies
nums[0...2] = [5, 8, 8] 5, then 8 [1, 2]
nums[2...4] = [8, 2, 2] 8, then 2 [1, 2]
nums[0...5] = [5, 8, 8, 2, 2, 2] 5, then 8, then 2 [1, 2, 3]

So the answer is 6 + 3 = 9.

Example 2:

Input: nums = [6,6,9,9]

Output: 5

Explanation:

Every single-element subarray has frequencies [1], and there are 4 of them. 1 longer subarray also qualifies:

Subarray Values by first appearance Frequencies
nums[1...3] = [6, 9, 9] 6, then 9 [1, 2]

So the answer is 4 + 1 = 5.

Example 3:

Input: nums = [2,3,4,4,3,4]

Output: 8

Explanation:

Every single-element subarray has frequencies [1], and there are 6 of them. 2 longer subarrays also qualify:

Subarray Values by first appearance Frequencies
nums[1...3] = [3, 4, 4] 3, then 4 [1, 2]
nums[0...5] = [2, 3, 4, 4, 3, 4] 2, then 3, then 4 [1, 2, 3]

So the answer is 6 + 2 = 8.

Constraints:

  • 1 <= nums.length <= 5 * 104
  • 1 <= nums[i] <= 105

Approach overview

Problem Overview: You get an integer array and must count the subarrays that satisfy the frequency staircase condition defined in the statement. The condition depends on how often each value appears inside a subarray, so you need to track counts as the subarray changes, not just its contents.

Approach 1: Brute Force Enumeration (O(n^2) time, O(n) space)

Fix a start index, then extend the end index one element at a time. Maintain a frequency map for the current subarray and check the staircase condition after each extension. This is correct because every subarray is visited exactly once. It becomes too slow once n reaches the upper constraints, since each check can take linear work when done naively. Use it to confirm your understanding of the condition before optimizing.

Approach 2: Sliding Window with Frequency Counts (O(n) amortized time, O(n) space)

Keep a window [left, right] and a hash map of counts for the window. Move right forward and update counts in O(1). When the window violates the staircase condition, advance left and decrement the outgoing count until the window is valid again. The key insight is that validity is preserved under the right kind of shrinking, so each index enters and leaves the window once. Count valid subarrays ending at each right using the window size or a derived offset, instead of recounting them. Prefer this approach when the condition is monotone with respect to subarray extension.

Approach 3: Frequency-of-Frequencies Tracking (O(n) time, O(n) space)

Track not only the count of each value but also how many values share each count, using a second hash map keyed by frequency. This lets you verify the staircase shape in O(1) per update, because the check reduces to looking at a few entries in the frequency-of-frequencies table. This removes the need to scan the window when testing validity. It is the cleanest version to implement once you understand Approach 2, and it is usually the one that holds up under tight interview time.

Recommended for interviews: Interviewers expect you to start with the brute force enumeration to show you understand the condition, then move to the sliding window with frequency tracking. The brute force shows understanding, but the optimal O(n) solution with frequency bookkeeping shows skill. Be ready to explain why each index is added and removed at most once. For background, review sliding window, hash table, and two pointers.

The written solution for this problem is still being prepared. Try it yourself in the meantime.

Try solving it yourself

Complexity analysis

ApproachTimeSpaceWhen to Use
Brute Force EnumerationO(n^2)O(n)Small inputs, verifying the condition
Sliding Window with Frequency CountsO(n) amortizedO(n)General case, monotone validity
Frequency-of-Frequencies TrackingO(n)O(n)Constant-time validity checks, interviews

Frequently asked questions

Is Count Frequency Staircase Subarrays II easy or hard?

It is a Hard problem. The difficulty comes from translating the staircase condition into frequency bookkeeping and keeping updates O(1) while the window shifts. Start with the brute force to build intuition before attempting the optimized window.

How to solve Count Frequency Staircase Subarrays II in O(n)?

Use two pointers to form a sliding window over the array. Update a hash map of counts as the right pointer advances, and shrink from the left whenever the staircase condition breaks. Count valid subarrays for each right endpoint during the scan.

Count Frequency Staircase Subarrays II Python and Java solution?

Both solutions follow the same sliding window pattern. In Python, use a dict or collections.Counter for frequencies. In Java, use HashMap<Integer, Integer> for counts and a second map for frequency-of-frequencies. Practice both on FleetCode.

What is the best approach for Count Frequency Staircase Subarrays II?

The best approach is a sliding window that maintains frequency counts, combined with a frequency-of-frequencies table for O(1) validity checks. Each index enters and leaves the window at most once. This gives O(n) time and O(n) space.

What data structure is used in Count Frequency Staircase Subarrays II?

A hash map is the core data structure, used to track how often each value appears in the current window. A second hash map keyed by frequency is often added to check the staircase shape in constant time.

What is the time complexity of Count Frequency Staircase Subarrays II?

The optimal solution runs in O(n) time with O(n) extra space for the frequency maps. The brute force enumeration of all subarrays runs in O(n^2) time.

Is Count Frequency Staircase Subarrays II asked at Google, Amazon, or Meta?

This problem is tagged as General on FleetCode and has a 16.5% acceptance rate, which marks it as a Hard problem. Check the company tags on your preferred platform for recent interview reports, since company tagging changes over time.

Comments

Now write it yourselfClose this page and solve Count Frequency Staircase Subarrays II in the editor, without looking.
Practise Count Frequency Staircase Subarrays II