2369. Check if There is a Valid Partition For The Array

Difficulty:
Related Topics:
Similar Questions:

    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:

    Return true** if the array has at least one valid partition**. Otherwise, return false.

      Example 1:

    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.
    

    Example 2:

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

      Constraints:

    Solution (Java)

    class Solution {
        public boolean validPartition(int[] nums) {
            boolean[] canPartition = new boolean[nums.length + 1];
            canPartition[0] = true;
            int diff = nums[1] - nums[0];
            boolean equal = diff == 0;
            boolean incOne = diff == 1;
            canPartition[2] = equal;
            for (int i = 3; i < canPartition.length; i++) {
                diff = nums[i - 1] - nums[i - 2];
                if (diff == 0) {
                    canPartition[i] = canPartition[i - 2] || (equal && canPartition[i - 3]);
                    equal = true;
                    incOne = false;
                } else if (diff == 1) {
                    canPartition[i] = incOne && canPartition[i - 3];
                    equal = false;
                    incOne = true;
                }
            }
            return canPartition[nums.length];
        }
    }
    

    Explain:

    nope.

    Complexity: