0

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

  1. The subarray consists of exactly 2 equal elements. For example, the subarray [2,2] is good.
  2. The subarray consists of exactly 3 equal elements. For example, the subarray [4,4,4] is good.
  3. 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:

  1. [4,4,4,5,6] - Valid (The array can be partitioned into the subarrays [4,4] and [4,5,6].)
  2. [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?

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065

1 Answers1

0

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 condition

the 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

We call a partition of the array valid if EACH OF THE OBTAINED SUBARRAYS satisfies one of the following conditions

That is why [1,1,1,2] do not have a valid partition and should return FALSE

SomannaK
  • 126
  • 1
  • 10