LeetCode 2369, Medium. Topics: Array, Dynamic Programming. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 32 parametrized pytest cases, and a playground notebook:
Problem
You are given a 0-indexed integer array nums. You have to partition the array into one or more contiguous subarrays.
We call a partition of the array valid if each of the obtained subarrays satisfies one of the following conditions:
- The subarray consists of exactly 2, equal elements. For example, the subarray
[2,2] is good.
- The subarray consists of exactly 3, equal elements. For example, the subarray
[4,4,4] is good.
- The subarray consists of exactly 3 consecutive increasing elements, that is, the difference between adjacent elements is
1. For example, the subarray [3,4,5] is good, but the subarray [1,3,5] is not.
Return true if the array has at least one valid partition. Otherwise, return false.
Examples
Constraints
- 2 <= nums.length <= 10^5
- 1 <= nums[i] <= 10^6
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
NeetCode All. Last modified on September 7, 2026