LeetCode每日一题(2369. Check if There is a Valid Partition For The Array)

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 elements, that is, the difference between adjacent elements is 1. For example, the subarray [3,4,5] is good, but the subarray [1,3,5] is not.
Return true if the array has at least one valid partition. Otherwise, return false.

Example 1:

Input: nums = [4,4,4,5,6]
Output: true

Explanation: The array can be partitioned into the subarrays [4,4] and [4,5,6].
This partition is valid, so we return true.

Example 2:

Input: nums = [1,1,1,2]
Output: false

Explanation: There is no valid partition for this array.

Constraints:

  • 2 <= nums.length <= 105
  • 1 <= nums[i] <= 106

假设 dp[i]为以 nums[i]为起点的结果, 那么我们需要检查以下几种情况:

  • 如果 nums[i] == nums[i+1] == nums[i+2], 此时如果 dp[i+3]为 true, 则整体结果为 true
  • 如果 nums[i] == nums[i+1], 此时如果 dp[i+2]为 true, 则整体结果为 true
  • 如果 nums[i+2] - nums[i+1] == 1 并且 nums[i+1] - nums[i] == 1, 此时如果 dp[i+3]为 true, 则整体结果为 true

注意考虑 nums[i]之后的元素不足 3 个的情况


use std::collections::HashMap;

impl Solution {
    fn dp(nums: &Vec<i32>, i: usize, cache: &mut HashMap<usize, bool>) -> bool {
        let length = nums.len();
        if i == length {
            return true;
        }
        if i == length - 1 {
            return false;
        }
        if i == length - 2 {
            return nums[length - 1] == nums[length - 2];
        }
        if nums[i] == nums[i + 1] && nums[i + 1] == nums[i + 2] {
            let next = if let Some(c) = cache.get(&(i + 3)) {
                *c
            } else {
                Solution::dp(nums, i + 3, cache)
            };
            if next {
                return true;
            }
        }
        if nums[i] == nums[i + 1] {
            let next = if let Some(c) = cache.get(&(i + 2)) {
                *c
            } else {
                Solution::dp(nums, i + 2, cache)
            };
            if next {
                return true;
            }
        }
        if nums[i + 2] - nums[i + 1] == 1 && nums[i + 1] - nums[i] == 1 {
            let next = if let Some(c) = cache.get(&(i + 3)) {
                *c
            } else {
                Solution::dp(nums, i + 3, cache)
            };
            if next {
                return true;
            }
        }
        cache.insert(i, false);
        false
    }
    pub fn valid_partition(nums: Vec<i32>) -> bool {
        Solution::dp(&nums, 0, &mut HashMap::new())
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值