<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:
        """
        neetcode solution. Tabulation.
        https://www.youtube.com/watch?v=OxXPiwWFdTI
        
        [  4,   4,   4,   5,   6] 
        [T/F, T/F, T/F, T/F, T/F]

        Fill this boolean array right to left.

        Base case, init:

            For the last value, there is no way
            that we can partition valid. 
            [  _,   _,   _,   _,   6] 
            [  _,   _,   _,   _,   F]

            For the last two values, we can simply
            check one condition (2 equal values)
            [  _,   _,   _,   5,   6] 
            [  _,   _,   _,   F,   F]

            For the last three values, we can simply
            check all two 3-value conditions (
            equal 3 values or increaing by 1)
            [  _,   _,   4,   5,   6] 
            [  _,   _,   T,   F,   F]

        For i:
            check all 3 conditions
            (i) two equal values
                       i     i+2
            E.g., [_, "4, 4", 5, 6]
                  [_,  _, _,  F, _]
            
            "4, 4" equal? and can we also 
            successfully partion from i+2? F

            (ii) three equal values
                       i         i+3
            E.g., [_, "4, 4, 5", 6]
                  [_,  _, _,  _, F]

            "4, 4, 5" equal? and can we also 
            successfully partion from i+3? F

            (iii) three increasing by 1
                       i         i+3
            E.g., [_, "4, 4, 5", 6]
                  [_,  _, _,  _, F]

            "4, 4, 5" increasing by 1? and 
            can we also successfully partion 
            from i+3? F

            any(i, ii, iii) = False.
            Hence, we put False.
                    i
            [  _,   4,   4,   5,   6] 
            [  _,   F,   T,   F,   F]

            --> shift i to left.

            check all 3 conditions
            (i) two equal values
                   i      i+2
            E.g., ["4, 4", 4, 5, 6]
                  [_,  F,  T, F, F]
            
            "4, 4" equal? and can we also 
            successfully partion from i+2? T

            (ii) three equal values
                   i         i+3
            E.g., ["4, 4, 4", 5, 6]
                  [ _, F, T,  F, F]

            "4, 4, 4" equal? and can we also 
            successfully partion from i+3? F

            (iii) three increasing by 1
                    i        i+3
            E.g., ["4, 4, 4", 5, 6]
                  [_,  F, T,  F,  F]

            "4, 4, 4" increasing by 1? and 
            can we also successfully partion 
            from i+3? F

            any(i, ii, iii) = True.
            Hence, we put True.
               i
            [  4,   4,   4,   5,   6] 
            [  T,   F,   T,   F,   F]

            return dp[i=0] = True.
        
        Time: O(n)
        Space: O(n) -> O(1)
        Notice we only need to keep
        i+2 and i+3, not the entire boolean 
        array.
        """
        # base case 
        two = nums[-1] == nums[-2]
        
        if len(nums) == 2:
            return two
        
        three = (
            (nums[-3] == nums[-2] == nums[-1]) or 
            # do we have three equal values?
            (nums[-3] + 1 == nums[-2] == nums[-1] - 1)
        )

        # we will only keep the three values.
        # space: O(n) -> O(1)
        dp = [three, two, False] 

        # once init is done, loop
        # nums = [4,  4, 4, 5, 6]
        # index      -4 -3 -2 -1

        for i in range(len(nums) - 4, -1, -1):
            # two values condition
            curr = (nums[i] == nums[i+1]) and dp[1]

            # three values condition
            three_conditon = (
                (nums[i] == nums[i+1] == nums[i+2]) or 
                (nums[i] + 1 == nums[i+1] == nums[i+2] - 1)
            )
            curr = curr or (three_conditon and dp[2])
            
            # shifting/overwriting
            # dp[0] was `three`
            # dp[1] was `two`
            # inserting curr at the front
            dp = [curr, dp[0], dp[1]]

        return dp[0]


    def validPartition_recur_memo(self, nums: List[int]) -> bool:
        """
        neetcode solution. Recursive memo.
        https://www.youtube.com/watch?v=OxXPiwWFdTI
        
        Good scenarios:
        (1) '22'  (size 2)
        (2) '444' (size 3)
        (3) '456' (size 3 and increasing by 1)
            check mid-1, mid, mid+1

        E.g.,
                   i 
                  [4,4,4,5,6] i=0
                    /   \\
        i=2     [4,4]   [4,4,4] i=3 
                /           \\
        i=5  [4,5,6]           [5,6] 
        
            Where @i=2
                i
            [4,4][4,5,6]

            (Q1) Are 4,5 equal? no
            (Q2) Are 4,5,6 equal? no
            (Q3) Are 4,5,6 increasing by 1? yes!
            move i=2 to i=5
            We found a valid solution.

            Where @i=3
                    i
            [4,4,4][5,6]
            (Q1) are 5,6 equal? no
            Cannot even ask Q2 and Q3 because
            there are not 3 numbers left.

        How did we get to @i? does not really matter
        It could be with 10 differnent partitions or whatever.
        Given the fact that we were able to partition 
        up to @i, we only care about the remaining numbers
        (remaining = subproblem!)
        This subprob is the same regarless of how we got there.
        --> memoization! with the index i that defines the 
        subproblem we are dealing with currently.

        i will map to True/False
        The # of subprob = O(n)
        Each subprob is O(1) operation because checking the
        fixed size array 2 or 3 is a constant operation.

        Time: O(n)
        Space: O(n)
        """

        def dfs(i):
            # base case: out of bounds
            # return True because 
            # we are able to split all the way
            if i >= len(nums):
                return True

            # base case: memo 
            if i in memo:
                return memo[i]

            # recursive case 

            check = False
            # do we have two equal values?
            if (
                # prevent out of bounds error
                i < len(nums) - 1 and 
                nums[i] == nums[i+1]
            ):
                # good, we now need to check
                # the subprob
                check = dfs(i+2)

            # prevent out of bounds error
            if i < len(nums) - 2:
                if (
                    # do we have three equal values?
                    (nums[i] == nums[i+1] == nums[i+2]) or 
                    # do we have three equal values?
                    (nums[i] + 1 == nums[i+1] == nums[i+2] - 1)
                ):
                    # good, we now need to check
                    # the subprob
                    check = check or dfs(i+3)
            
            memo[i] = check
            return check

        memo = {}
        return dfs(0)
771