[Python solution] 2369. Check the effective partitioning method of the array
Question overview
Leetcode question 2369 asks us to check an array of integersnumsWhether it can be divided into one or more consecutive subarrays that meet certain conditions. Valid partition conditions include:
- A subarray consists of two equal elements, such as
[2, 2]。 - A subarray consists of three equal elements, such as
[3, 3, 3]。 - The subarray consists of three consecutively increasing elements, and the difference between adjacent elements is 1, such as
[4, 5, 6]。
Returns if there is at least one valid partition of the arraytrue; Otherwise returnfalse。
Example analysis
- Example 1:
nums = [4, 4, 4, 5, 6]can be divided into[4, 4]and[4, 5, 6], so returntrue。 - Example 2:
nums = [1, 1, 1, 2]None of the partitioning conditions are met, so returnfalse。
Problem-solving ideas
adoptdynamic programmingstrategy, we define a Boolean arraydp, among whichdp[i]Represents the front of the arrayiWhether the elements can be divided effectively. Iterate over the array, for each positioni, try the following three division methods:
- The subarray consists of the last two elements that are equal.
- The subarray consists of the last three equal elements.
- The subarray consists of the last three consecutively increasing elements.
Algorithm process
- Initialize dp[0] to true, indicating that the empty array can be effectively divided.
- For each i (starting at 2), check whether the array can be partitioned into i in one of the three ways above.
- Ultimately, dp[n] (where n is the length of the array) gives the answer to whether the entire array can be efficiently divided.
- If either way is possible, then
dp[i]set totrue。
Specifically
Initialization:
- Initialize dp[0] to true, indicating that the empty array can be effectively divided.
- State transition: For each element nums[i] in the array (starting from the second element), consider the following situations:
- If nums[i] is equal to nums[i-1], check whether dp[i-2] is true. If so, it means that [nums[i-1], nums[i]] can form a valid partition, and set dp[i] to true.
- If nums[i], nums[i-1] and nums[i-2] are equal, dp[i-3] is also checked. If dp[i-3] is true, it means [nums[i-2], nums[i-1], nums[i]] can form an effective division, set dp[i] to true.
- If nums[i], nums[i-1] and nums[i-2] form a continuous increasing sequence (i.e. nums[i]-1 == nums[i-1] and nums[i-1]-1 == nums[i-2]), check dp[i-3]. If true, set dp[i] to true.
- Result judgment:
- Finally, the value of dp[n] is checked (where n is the length of the array nums). If dp[n] is true, it means that the entire array can be effectively divided; otherwise, it cannot.
Dynamic programming implementation
1 | class Solution: |
Algorithm complexity analysis
- time complexity:O(N), where N is an array
numslength, the entire array needs to be traversed to fill the dynamic programming array. - space complexity:O(N), used to store dynamic programming arrays
dp, record the division of each position.
Through dynamic programming, we provide an efficient and intuitive solution that ensures optimal performance of the algorithm for similar array partitioning problems.