<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:
        valid = ([False] * len(nums)) + [True]

        def tabulation():
            if nums[-2] == nums[-1]:
                valid[-3] = True
            for i in range(len(nums)-3, -1, -1):
                if valid[i + 2]:
                    if nums[i] == nums[i + 1]:
                        valid[i] = True
                if valid[i + 3]:
                    if nums[i] == nums[i + 1] and nums[i + 1] == nums[i + 2]:
                        valid[i] = True
                    if nums[i] + 1 == nums[i + 1] and nums[i + 1] + 1 == nums[i + 2]:
                        valid[i] = True
            return valid[0]




        # def recursive():
        #     valid = [None] * (len(nums) + 1)
        #     valid[len(nums)] = True
        #     hits = { 'val': 0 }
            
        #     def valid_from(start: int):
        #         print('valid_from', start)
        #         if valid[start] is not None:
        #             hits['val'] += 1
        #             print('hit', start, valid[start])
        #             return valid[start]
        #         if start == len(nums):
        #             return True
        #         count = 0
        #         for i in range(start, len(nums)):
        #             count += 1
        #             if count == 1:
        #                 continue
        #             can_end_partition = False
        #             if count == 2:
        #                 if nums[i] == nums[i-1]:
        #                     print('can 2', start)
        #                     can_end_partition = True
        #             elif count == 3:
        #                 if nums[i - 2] == nums[i - 1] and nums[i - 1] == nums[i]:
        #                     print('can 3', start)
        #                     can_end_partition = True
        #                 elif nums[i - 2] + 1 == nums[i - 1] and nums[i - 1] + 1 == nums[i]:
        #                     print('can 3', start)
        #                     can_end_partition = True
        #                 else:
        #                     print('cant', start)
        #                     valid[start] = False
        #                     return False
        #             if can_end_partition and valid_from(i + 1):
        #                 print('can', start)
        #                 valid[start] = True
        #                 return True
        #         print('cant', start)
        #         valid[start] = False
        #         return False


        #     res = valid_from(0)
        #     print('hits', hits['val'])
        #     return res

        return tabulation()

728