<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p>

<p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p>

<ol>
	<li>The subarray consists of <strong>exactly</strong> <code>2,</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li>
	<li>The subarray consists of <strong>exactly</strong> <code>3,</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li>
	<li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li>
</ol>

<p>Return <code>true</code><em> if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

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

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,1,1,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no valid partition for this array.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
</ul>

class Solution:
    def validPartition(self, nums: List[int]) -> bool:
        # bottom up DP approach
        # subproblem: if we can partition nums correctly starting at an index i
        # base cases: canPartition[-1] = False because valid subarrays are of size 2 or 3
        # canPartition[-2] = True if isValid(nums[i:i+2]), else false
        # canPartition[-3] = True if isValid(nums[i:i+3]), else false

        # recurrence:
        # canPartition[i] = True if isValid(nums[i:i+2]) and canPartition[i+2] or
        #                   if isValid(nums[i:i+3]) and canPartition[i+3] else False


        def isValid(nums):
            if len(nums) == 2:
                return nums[0] == nums[1]

            if len(nums) == 3:
                if nums[0] == nums[1] and nums[0] == nums[2]:
                    return True
                
                if nums[0] == (nums[1] - 1) and nums[0] == (nums[2] - 2):
                    return True
            
            return False
        
        if len(nums) <= 3:
            return isValid(nums)

        dp = [False] * len(nums)

        # base cases
        dp[-1] = False
        dp[-2] = isValid(nums[-2:])
        dp[-3] = isValid(nums[-3:])

        # recurrence
        for i in range(len(dp) - 4, -1, -1):
            # if the next subarray is of size 2 and valid
            if isValid(nums[i : i + 2]) and dp[i + 2]:
                dp[i] = True
            
            # if the next subarray if of size 3 and valid and we can create a valid partition starting after i + 3
            elif isValid(nums[i : i + 3]) and dp[i + 3]:
                dp[i] = True
            
            else:
                dp[i] = False
        
        return dp[0]

838