当前位置:网站首页>力扣练习——26 分割数组为连续子序列
力扣练习——26 分割数组为连续子序列
2022-07-22 07:48:00 【qq_43403657】
26 分割数组为连续子序列
1.问题描述
给你一个按升序排序的整数数组 num(可能包含重复数字),请你将它们分割成一个或多个子序列,其中每个子序列都由连续整数组成且长度至少为 3 。
一个子序列是从原始数组挑选一部分(也可以全部)元素而不改变相对位置形成的新数组
如果可以完成上述分割,则返回 true ;否则,返回 false 。
示例 1:
输入: [1,2,3,3,4,5]
输出: True
解释:
你可以分割出这样两个连续子序列 :
1, 2, 3
3, 4, 5
示例 2:
输入: [1,2,3,3,4,4,5,5]
输出: True
解释:
你可以分割出这样两个连续子序列 :
1, 2, 3, 4, 5
3, 4, 5
示例 3:
输入: [1,2,3,4,4,5]
输出: False
说明:
输入的数组长度范围为 [1, 10000]
2.输入说明
首先输入num的长度n,然后输入n个整数
3.输出说明
输出“true”或“false”,不包括引号。
4.范例
输入
8
1 2 3 3 4 4 5 5
输出
true
5.代码
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<unordered_map>
using namespace std;
bool isPossible(vector<int>& nums) {
//1.使用两个哈希表nc和tail
//cnt[i] 存储原数组中数字i出现的次数
//tail[i]存储以i结尾且符合题意的连续子序列的个数
unordered_map<int, int> cnt, tail;//unordered_map:其实现使用的是哈希表 ;而 map实现使用的是红黑树
//2.统计出现次数
for (auto num : nums)
cnt[num]++;
//3.遍历 只有检查到某个数时,这个数未被消耗完,且既不能和前面组成连续子序列,也不能和后面组成连续子序列时,无法分割
for (auto num : nums)
{
if (cnt[num] == 0) continue;
else if (cnt[num] > 0 && tail[num - 1] > 0) //tail[num-1]>0表示前面的连续子序列的尾部到num-1 ;如果此时 num 至少有一个 ,则可以延申该子序列
{
cnt[num]--;//num出现次数减一
tail[num - 1]--;//以num-1结尾的连续子序列个数减一
tail[num]++;//以num结尾的连续子序列个数加一
}
else if (cnt[num] > 0 && cnt[num + 1] > 0 && cnt[num + 2] > 0) //存在连续子序列
{
cnt[num]--;
cnt[num + 1]--;
cnt[num + 2]--;
tail[num + 2]++;
}
else
return false;
}
return true;
}
int main()
{
int n,tmp;
cin >> n;
vector<int>nums;
for (int i = 0; i < n; i++)
{
cin >> tmp;
nums.push_back(tmp);
}
bool res = isPossible(nums);
//true返回值为1 false 返回值为0
res == 1 ? (cout << "true") : (cout << "false");
return 0;
}
边栏推荐
- 为什么有些参数reload就可以生效,而有些参数必须重启数据库?
- Branch and loop statements
- 栈/堆/队列刷题(上)
- MySQL addition, deletion, modification and query (Advanced)
- RK3399平台开发系列讲解(ALSA子系统)4.37、ALSA驱动框架
- Pytoch implements word2vec
- How to make an appointment while watching the panorama? Here comes the VR catering system tutorial
- impdp content=data_ Only can you choose to skip or overwrite when there are records?
- Intent 跳转 传递list集合
- MySQL constraints
猜你喜欢
web3分享
ieee下载文献的方法
(11) 51 Single Chip Microcomputer -- realize the storage of stopwatch data with AT24C02 (attached with achievement display)
【Harmony OS】【ARK UI】【Demo】加载动画实现
Branch and loop statements
Focus on the "double five" project, directly hit the front line of the project - Xiangjiang new area, and rise at the top of the industry
Data link layer of network (PPP Protocol)
Hcip OSPF interface network type experiment report
数据湖(十八):Flink与Iceberg整合SQL API操作
Abnormal understanding and learning
随机推荐
How does Oracle set up not to check compilation errors during creation?
go 语言 结构体如何申明默认值 如何转化为json数据
MySQL 增删改查(進階)
【微服务~远程调用】整合RestTemplate、WebClient、Feign
Install vscode offline
Do all Navicat versions support MySQL? Why can't I open the connection?
[micro Service ~ remote call] integrate resttemplate, webclient, feign
24、 TF coordinate transformation (IV): multi coordinate transformation and coordinate system relationship view
分布式(一)分布式系统,BASE,CAP是何方神圣?
vscode 安装 tools失败
The problem of the result set returned by MySQL stored procedure
Request message details (request header, get, post, request body)
【云原生】Docker部署数据库的持久化
Excel导入导出Controller
Can flick SQL query Clickhouse
深度学习(二)一文带你了解神经网络,激活函数
Understanding and learning of arrays class
【HMS core】【FAQ】【Account Kit】典型问题合集1
[HMS core] [FAQ] in app purchases FAQ sharing
【论文汇总】2D目标检测文章汇总,持续更新