2856. Count Complete Subarrays in an Array
My accepted Python solution to LeetCode problem 2856, Count Complete Subarrays in an Array, running in 491ms.
- Difficulty: Medium
- Python
- Runtime 491ms
- Memory 17.4MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 491ms, memory 17.4MB, accepted 2025-12-31.
class Solution:
def countCompleteSubarrays(self, nums: List[int]) -> int:
total_distinct = len(set(nums))
n = len(nums)
result = 0
for i in range(n):
seen = set()
for j in range(i, n):
seen.add(nums[j])
if len(seen) == total_distinct:
# All subarrays from i to j, j+1, ..., n-1 are complete
result += n - j
break
return result