552. Check if There is a Valid Partition for the Array

HardBinary SearchDPArray

Given an array, decide whether it can be partitioned into contiguous blocks where each block is one of: exactly two equal elements; exactly three equal elements; or exactly three consecutive increasing elements (each one greater than the previous by exactly 1). Return true if such a partition exists, otherwise false. Input: an array of integers.

Input: An array of integers.

Output: Boolean — true or false.

Examples

Example 1
Input: [4,4,4,5,6]
Output: true
Explanation: [4,4] and [4,5,6].
Example 2
Input: [1,1,1,2]
Output: false
Explanation: No valid partition.
Example 3
Input: [1,2,3,4]
Output: false
Explanation: Cannot tile into valid blocks.

Constraints

Asked by

Google
Solve this problem in the editor →