I was trying to solve this problem on leetcode.
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 element, For example, the subarray [3,4,5] is good
The problem is that there are two examples:
- [4,4,4,5,6] - Valid (The array can be partitioned into the subarrays [4,4] and [4,5,6].)
- [1,1,1,2] - Invalid
I expected example two to be valid as well since [1,1] and [1,1,1] both satisfy conditions 1 and 2 respectively.
So, why is it invalid?
so there are three conditions and all the sub-array that you get after partition must satisfy at least one of the conditions.
for the example
[1,1,1,2], there are three ways you can make the partition and as you already know[1] and [1,1,2]do not satisfy any conditionthe other two partitions left are
[1,1] and [1,2]and[1,1,1] and [2]. In these situations,[1,1]and[1,1,1]indeed do satisfy one of the conditions but[1,2]and[2]do not satisfy any condition.And for a partition of the array to be valid
That is why [1,1,1,2] do not have a valid partition and should return
FALSE