https://leetcode.com/problems/check-if-there-is-a-valid-partition-for-the-array/description/
2369. Check if There is a Valid Partition For The Array
给你一个阵列nums,求出是否可以把该阵列切成若干个子阵列,这些阵列需要满足至少
一个下列的三个条件:
1.子阵列大小为2且元素相同,例如:[2,2]
2.子阵列大小为3且元素相同,例如:[2,2,2]
3.子阵列大小为3,满足相差为1且递增,例如:[1,2,3]
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.
思路:
1.用动态规划,每次要切分 i 之前都去检查 i - 2 或 i - 3 前面的阵列是否存在
合法的切法。
2.最后返回 dp[n] 即可。
Java Code: