Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/2369.html

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

  • Difficulty: Medium.
  • Related Topics: Array, Dynamic Programming.
  • 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:

  • 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.

  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:

  • 2 <= nums.length <= 105

  • 1 <= nums[i] <= 106

Solution (Java, C++, Python)

  • 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];
        }
    }
    
    ############
    
    class Solution {
        private int n;
        private int[] f;
        private int[] nums;
    
        public boolean validPartition(int[] nums) {
            this.nums = nums;
            n = nums.length;
            f = new int[n];
            Arrays.fill(f, -1);
            return dfs(0);
        }
    
        private boolean dfs(int i) {
            if (i == n) {
                return true;
            }
            if (f[i] != -1) {
                return f[i] == 1;
            }
            boolean res = false;
            if (i < n - 1 && nums[i] == nums[i + 1]) {
                res = res || dfs(i + 2);
            }
            if (i < n - 2 && nums[i] == nums[i + 1] && nums[i + 1] == nums[i + 2]) {
                res = res || dfs(i + 3);
            }
            if (i < n - 2 && nums[i + 1] - nums[i] == 1 && nums[i + 2] - nums[i + 1] == 1) {
                res = res || dfs(i + 3);
            }
            f[i] = res ? 1 : 0;
            return res;
        }
    }
    
  • class Solution:
        def validPartition(self, nums: List[int]) -> bool:
            @cache
            def dfs(i):
                if i == n:
                    return True
                res = False
                if i < n - 1 and nums[i] == nums[i + 1]:
                    res = res or dfs(i + 2)
                if i < n - 2 and nums[i] == nums[i + 1] and nums[i + 1] == nums[i + 2]:
                    res = res or dfs(i + 3)
                if (
                    i < n - 2
                    and nums[i + 1] - nums[i] == 1
                    and nums[i + 2] - nums[i + 1] == 1
                ):
                    res = res or dfs(i + 3)
                return res
    
            n = len(nums)
            return dfs(0)
    
    ############
    
    # 2369. Check if There is a Valid Partition For The Array
    # https://leetcode.com/problems/check-if-there-is-a-valid-partition-for-the-array/
    
    class Solution:
        def validPartition(self, nums: List[int]) -> bool:
            n = len(nums)
            
            @cache
            def go(index):
                if index >= n:
                    return True
                
                valid = False
                
                if index + 1 < n and nums[index] == nums[index + 1]:
                    valid |= go(index + 2)
                    
                if index + 2 < n and nums[index] == nums[index + 1] == nums[index + 2]:
                    valid |= go(index + 3)
                    
                if index + 2 < n and nums[index] + 1 == nums[index + 1] and nums[index + 1] + 1 == nums[index + 2]:
                    valid |= go(index + 3)
                
                return valid
                
            return go(0)
    
    
  • class Solution {
    public:
        vector<int> f;
        vector<int> nums;
        int n;
    
        bool validPartition(vector<int>& nums) {
            n = nums.size();
            this->nums = nums;
            f.assign(n, -1);
            return dfs(0);
        }
    
        bool dfs(int i) {
            if (i == n) return true;
            if (f[i] != -1) return f[i] == 1;
            bool res = false;
            if (i < n - 1 && nums[i] == nums[i + 1]) res = res || dfs(i + 2);
            if (i < n - 2 && nums[i] == nums[i + 1] && nums[i + 1] == nums[i + 2]) res = res || dfs(i + 3);
            if (i < n - 2 && nums[i + 1] - nums[i] == 1 && nums[i + 2] - nums[i + 1] == 1) res = res || dfs(i + 3);
            f[i] = res ? 1 : 0;
            return res;
        }
    };
    
  • func validPartition(nums []int) bool {
    	n := len(nums)
    	f := make([]int, n)
    	for i := range f {
    		f[i] = -1
    	}
    	var dfs func(int) bool
    	dfs = func(i int) bool {
    		if i == n {
    			return true
    		}
    		if f[i] != -1 {
    			return f[i] == 1
    		}
    		res := false
    		f[i] = 0
    		if i < n-1 && nums[i] == nums[i+1] {
    			res = res || dfs(i+2)
    		}
    		if i < n-2 && nums[i] == nums[i+1] && nums[i+1] == nums[i+2] {
    			res = res || dfs(i+3)
    		}
    		if i < n-2 && nums[i+1]-nums[i] == 1 && nums[i+2]-nums[i+1] == 1 {
    			res = res || dfs(i+3)
    		}
    		if res {
    			f[i] = 1
    		}
    		return res
    	}
    	return dfs(0)
    }
    
  • function validPartition(nums: number[]): boolean {
        const n = nums.length;
        const vis = new Array(n).fill(false);
        const queue = [0];
        while (queue.length !== 0) {
            const i = queue.shift() ?? 0;
    
            if (i === n) {
                return true;
            }
    
            if (!vis[i + 2] && i + 2 <= n && nums[i] === nums[i + 1]) {
                queue.push(i + 2);
                vis[i + 2] = true;
            }
    
            if (
                !vis[i + 3] &&
                i + 3 <= n &&
                ((nums[i] === nums[i + 1] && nums[i + 1] === nums[i + 2]) ||
                    (nums[i] === nums[i + 1] - 1 &&
                        nums[i + 1] === nums[i + 2] - 1))
            ) {
                queue.push(i + 3);
                vis[i + 3] = true;
            }
        }
        return false;
    }
    
    

Explain:

nope.

Complexity:

  • Time complexity : O(n).
  • Space complexity : O(n).

All Problems

All Solutions