> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Check if There is a Valid Partition For The

> Tested Python solution for LeetCode 2369 with 32 pytest cases. Generate a practice environment with lcpy.

LeetCode 2369, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Dynamic Programming](/catalog/topics/dynamic-programming). [View on LeetCode](https://leetcode.com/problems/check-if-there-is-a-valid-partition-for-the-array/description/).

Generate this problem as a practice environment: tested reference solution, 32 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2369   # by problem number
lcpy gen -s check_if_there_is_a_valid_partition_for_the_array   # by problem name
```

## 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

```
Input: nums = [4,4,4,5,6]
Output: true
Explanation: The array can be partitioned into the subarrays [4,4] and [4,5,6].
This partition is valid, so we return true.
```

```
Input: nums = [1,1,1,2]
Output: false
Explanation: There is no valid partition for this array.
```

### Constraints

* 2 \<= nums.length \<= 10^5
* 1 \<= nums\[i] \<= 10^6

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/check_if_there_is_a_valid_partition_for_the_array/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/check_if_there_is_a_valid_partition_for_the_array/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def valid_partition(self, nums: list[int]) -> bool:
        # dp over the last three prefix results, rolling to constant space
        dp2 = False  # can partition nums[:i-3]
        dp1 = True  # can partition nums[:i-2]
        dp0 = False  # can partition nums[:i-1]
        n = len(nums)
        for i in range(2, n + 1):
            nxt = False
            if dp1 and nums[i - 1] == nums[i - 2]:
                nxt = True
            elif i >= 3 and dp2:
                a, b, c = nums[i - 3], nums[i - 2], nums[i - 1]
                if (a == b == c) or (a + 1 == b and b + 1 == c):
                    nxt = True
            dp2, dp1, dp0 = dp1, dp0, nxt
        return dp0
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.