首页 > 其他分享 >[LeetCode] 2369. Check if There is a Valid Partition For The Array

[LeetCode] 2369. Check if There is a Valid Partition For The Array

时间:2023-08-14 13:24:28浏览次数:39  
标签:Partition nums There 数组 subarray array true Check dp

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

检查数组是否存在有效划分。

给你一个下标从 0 开始的整数数组 nums ,你必须将数组划分为一个或多个 连续 子数组。

如果获得的这些子数组中每个都能满足下述条件 之一 ,则可以称其为数组的一种 有效 划分:

  1. 子数组 恰 由 2 个相等元素组成,例如,子数组 [2,2] 。
  2. 子数组 恰 由 3 个相等元素组成,例如,子数组 [4,4,4] 。
  3. 子数组 恰 由 3 个连续递增元素组成,并且相邻元素之间的差值为 1 。例如,子数组 [3,4,5] ,但是子数组 [1,3,5] 不符合要求。

如果数组 至少 存在一种有效划分,返回 true ,否则,返回 false 。

思路是动态规划,有点像爬楼梯那一类的题目。假设 input 数组的长度为 n,那么这里我也创建一个长度为 n + 1 的 dp 数组,dp 数组的定义为走到位置 i 的时候,这个长度为 i 的子数组能否组成一个有效划分。是否可以被有效划分的定义参照如上三点,有三种不同情况,可直接参见代码。

时间O(n)

空间O(n)

Java实现

 1 class Solution {
 2     public boolean validPartition(int[] nums) {
 3         var n = nums.length;
 4         var dp = new boolean[n + 1];
 5         dp[0] = true;
 6         for (var i = 1; i < n; ++i)
 7             if (dp[i - 1] && nums[i] == nums[i - 1] ||
 8                 i > 1 && dp[i - 2] && (nums[i] == nums[i - 1] && nums[i] == nums[i - 2] ||
 9                 nums[i] == nums[i - 1] + 1 && nums[i] == nums[i - 2] + 2)) {
10                     dp[i + 1] = true;
11                 }
12         return dp[n];
13     }
14 }

 

LeetCode 题目总结

标签:Partition,nums,There,数组,subarray,array,true,Check,dp
From: https://www.cnblogs.com/cnoodle/p/17628363.html

相关文章

  • ethereum错误之nonce too low
    根据提供的错误信息error(*github.com/ethereum/go-ethereum/rpc.jsonError)*{Code:-32000,Message:"noncetoolow",Data:interface{}nil},这是一个来自以太坊的JSON-RPC错误。该错误的含义是“noncetoolow”,即“交易序号(nonce)过低”。在以太坊中,每个账户都有一个交......
  • ethereum错误之already known
    根据提供的错误信息error(*github.com/ethereum/go-ethereum/rpc.jsonError)*{Code:-32000,Message:"alreadyknown",Data:interface{}nil},这是一个来自以太坊的JSON-RPC错误。该错误的含义是“alreadyknown”,即“已经存在”。在以太坊中,当您尝试发送一个已经存在于区......
  • dimp V8:[WARNING]login fail, check your username and password, and check the serv
    在进行某个项目的性能测试时,我们选择了达梦8作为使用的数据库。前期是在一台功能测试环境的达梦数据库服务上创建用于压力测试的业务数据。后续将数据库导出,并导入一台专门做性能测试的高性能服务器(部署同样版本的达梦8),执行数据库文件导入操作时遇到了问题。以下是出现的错误......
  • 分区函数Partition By的用法
    原文地址:https://blog.csdn.net/locken123/article/details/127411319前言:partitionby关键字是分析性函数的一部分,它和聚合函数(如groupby)不同的地方在于它能返回一个分组中的多条记录,而聚合函数一般只有一条反映统计值的记录。partitionby用于给结果集分组,如果没有指定那么它......
  • yum update更新报错 Transaction Check Error 解决方法
    yumupdate更新报错TransactionCheckError解决方法yumupdate引起错误TransactionCheckError的原因很多,要根据错误概要去判断具体原因。错误现象:报错内容:file/usr/share/man/man1/gtk-query-immodules-2.0.1.gzfrominstallofgtk2-2.24.31-1.el7.x86_64conflicts......
  • springbatch remote partition
    SpringBatch远程分区demo*使用框架版本<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.han</groupId> <......
  • C# 读取带CheckBox复选框控件的表格-并集成到Windows Service里面
    最近的项目要求读取xls文件内的单元格,并且单元格旁边会有复选框标识类型。搜了下只有java的POI有例子,NOPI看项目文档好像是没有实现读取控件的功能。java实现POI POI如何解析出excel中复选框是否被选中https://blog.csdn.net/qq_29832217/article/details/104413475 C#导......
  • Flink Unaligned Checkpoint 在 Shopee 的优化和实践
    一、Checkpoint存在的问题1.1数据库的Snapshot和恢复机制首先看一下数据库的Snapshot和恢复机制。数据库周期性地快照,每一次Snapshot是一个全量快照,同时要持续地写ChangeLog或WAL。当数据库crash后,新的数据库实例会从Snapshot3以及最新的ChangeLog恢复。1.2Fli......
  • [async]子线程内开启协程 RuntimeError: There is no current event loop in thread '
    在子线程内直接获取事件循环会报错:RuntimeError:Thereisnocurrenteventloopinthread'Thread-2',此时的代码为:loop=asyncio.get_event_loop()loop.run_until_complete(协程函数) #执行解决方法:在子线程内创建并配置事件循环new_loop=asyncio.new_event_loop(......
  • Flink调优-Checkpoint优化
    1Checkpoint的介绍Checkpoint的官网文档地址:https://nightlies.apache.org/flink/flink-docs-release-1.17/zh/docs/ops/state/checkpoints/Checkpoint使Flink的状态具有良好的容错性,通过checkpoint机制,Flink可以对作业的状态和计算位置进行恢复。Flink是一个分布式的流处理......