2811. Check if it is Possible to Split Array
Description
You are given an array nums
of length n
and an integer m
. You need to determine if it is possible to split the array into n
arrays of size 1 by performing a series of steps.
An array is called good if:
- The length of the array is one, or
- The sum of the elements of the array is greater than or equal to
m
.
In each step, you can select an existing array (which may be the result of previous steps) with a length of at least two and split it into two arrays, if both resulting arrays are good.
Return true if you can split the given array into n
arrays, otherwise return false.
Example 1:
Input: nums = [2, 2, 1], m = 4
Output: true
Explanation:
- Split
[2, 2, 1]
to[2, 2]
and[1]
. The array[1]
has a length of one, and the array[2, 2]
has the sum of its elements equal to4 >= m
, so both are good arrays. - Split
[2, 2]
to[2]
and[2]
. both arrays have the length of one, so both are good arrays.
Example 2:
Input: nums = [2, 1, 3], m = 5
Output: false
Explanation:
The first move has to be either of the following:
- Split
[2, 1, 3]
to[2, 1]
and[3]
. The array[2, 1]
has neither length of one nor sum of elements greater than or equal tom
. - Split
[2, 1, 3]
to[2]
and[1, 3]
. The array[1, 3]
has neither length of one nor sum of elements greater than or equal tom
.
So as both moves are invalid (they do not divide the array into two good arrays), we are unable to split nums
into n
arrays of size 1.
Example 3:
Input: nums = [2, 3, 3, 2, 3], m = 6
Output: true
Explanation:
- Split
[2, 3, 3, 2, 3]
to[2]
and[3, 3, 2, 3]
. - Split
[3, 3, 2, 3]
to[3, 3, 2]
and[3]
. - Split
[3, 3, 2]
to[3, 3]
and[2]
. - Split
[3, 3]
to[3]
and[3]
.
Constraints:
1 <= n == nums.length <= 100
1 <= nums[i] <= 100
1 <= m <= 200
Solutions
Solution 1: Memoization Search
First, we preprocess to get the prefix sum array $s$, where $s[i]$ represents the sum of the first $i$ elements of the array $nums$.
Next, we design a function $dfs(i, j)$, which represents whether there is a way to split the index range $[i, j]$ of the array $nums$ that meets the conditions. If it exists, return true
, otherwise return false
.
The calculation process of the function $dfs(i, j)$ is as follows:
If $i = j$, then there is only one element, no need to split, return true
;
Otherwise, we enumerate the split point $k$, where $k \in [i, j]$, if the following conditions are met, then it can be split into two subarrays $nums[i,.. k]$ and $nums[k + 1,.. j]$:
- The subarray $nums[i,..k]$ has only one element, or the sum of the elements of the subarray $nums[i,..k]$ is greater than or equal to $m$;
- The subarray $nums[k + 1,..j]$ has only one element, or the sum of the elements of the subarray $nums[k + 1,..j]$ is greater than or equal to $m$;
- Both $dfs(i, k)$ and $dfs(k + 1, j)$ are
true
.
To avoid repeated calculations, we use the method of memoization search, and use a two-dimensional array $f$ to record all the return values of $dfs(i, j)$, where $f[i][j]$ represents the return value of $dfs(i, j)$.
The time complexity is $O(n^3)$, and the space complexity is $O(n^2)$, where $n$ is the length of the array $nums$.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Solution 2: Quick Thinking
No matter how you operate, there will always be a length == 2
subarray left in the end. Since there are no negative numbers in the elements, as the split operation proceeds, the length and sum of the subarray will gradually decrease. The sum of other length > 2
subarrays must be larger than the sum of this subarray. Therefore, we only need to consider whether there is a length == 2
subarray with a sum greater than or equal to m
.
📢 Note that when
nums.length <= 2
, no operation is needed.
The time complexity is $O(n)$, where $n$ is the length of the array $nums$. The space complexity is $O(1)$.
1 2 3 4 5 6 7 8 9 10 11 12 |
|