连续子数组和非连续

问题描述 投票:0回答:1

我试图在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

问题是有两个例子:

  1. [4,4,4,5,6] - 有效(数组可以分为子数组 [4,4] 和 [4,5,6]。)
  2. [1,1,1,2] - 无效

我希望示例 2 也有效,因为 [1,1] 和 [1,1,1] 分别满足条件 1 和 2。

那么,为什么无效呢?

data-structures computer-science partition-problem
1个回答
0
投票

所以存在三个条件,分区后得到的所有子数组必须至少满足其中一个条件。

对于示例

[1,1,1,2]
,您可以通过三种方式进行分区,并且您已经知道
[1] and [1,1,2]
不满足任何条件

剩下的另外两个分区是

[1,1] and [1,2]
[1,1,1] and [2]
。在这些情况下,
[1,1]
[1,1,1]
确实满足其中一个条件,但
[1,2]
[2]
不满足任何条件。

并且数组的分区有效

如果获得的每个子数组满足以下条件之一,我们称数组的分区有效

这就是为什么 [1,1,1,2] 没有有效分区并且应该返回

FALSE

© www.soinside.com 2019 - 2024. All rights reserved.